Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

BigPrince

macrumors 68020
Original poster
Dec 27, 2006
2,053
111
ok so I have a program where the user inputs numbers from a keyboard. When they are done I have them press CTRL-Z, which on a PC represents EOF. Messing around with X-code, this is not the case. What do I have to press on a for OS X?
 

bousozoku

Moderator emeritus
Jun 25, 2002
16,120
2,397
Lard
Control-D should work on any UNIX machine for end of file.

By the way, it's Xcode. ;)
 

BigPrince

macrumors 68020
Original poster
Dec 27, 2006
2,053
111
yaya, i keep screwing up the name, sue me.

thx for the help though


edit:Control-D would not do it
 

kpua

macrumors 6502
Jul 25, 2006
294
0
Unless I'm missing something I've noticed that Xcode's shell emulation is not up to par and won't catch many of the Control sequences. The only solution that I'm aware of is to just use Terminal.app (open the Products folder and double click the executable to get to it the quickest). Unfortunately, this makes debugging very difficult when dealing with these Control sequences...
 

BigPrince

macrumors 68020
Original poster
Dec 27, 2006
2,053
111
ok so how can the user tell the program they are done inputting their values from the keyboard?
 

BigPrince

macrumors 68020
Original poster
Dec 27, 2006
2,053
111
doesnt work.

however if I input an odd charcter like ¿ then it automatically stops allowing the user to input and moves onto the next part of the program.
 

toddburch

macrumors 6502a
Dec 4, 2006
748
0
Katy, Texas
doesnt work.

What about looking for an empty ENTER press would not work? What language are you writing in?

Code:
#include <stdio.h>

int main (int argc, const char * argv[]) {
    FILE *file_pointer ;   /* declare the file handler / stream pointer. */  
	char input_char, last_char  ; 
	
	file_pointer = fopen("memo.txt" , "r") ;   /* this time read the file. */   
	if (file_pointer == NULL) 
	{ 	
		printf("File does not exist.  Skipping the READ.\n") ; 
	}
	else 
	{
	    printf("The old contents of memo.txt are:\n") ; 

		while (input_char != EOF )  
		{
			input_char = getc( file_pointer ) ; 
			if (input_char != EOF) printf("%c" , input_char ) ; 
		} 

		printf("\n...End of memo.\n") ; 
		fclose(file_pointer) ; 
	}
	file_pointer = fopen("memo.txt", "w") ;   /* open the file. Get a pointer. */ 
	printf("Enter Memo...\n") ; 
	int double_enter = 0 ;  
	input_char = ' ' ; 
	while ( double_enter==0 )    /*  do it until enter is pressed twice. */   
	{
		if (last_char == '\n' && input_char == '\n') double_enter = 1 ; 
		else 
		{
			last_char = input_char ; 
			scanf("%c", &input_char) ;         /* must pass the address of the variable */  
			putc( input_char, file_pointer) ;  /* putc updates file_pointer with the next address when finished.  */   
		}
	}
	fclose(file_pointer) ;	
	return 0;
}
 

BigPrince

macrumors 68020
Original poster
Dec 27, 2006
2,053
111
I am just starting out in C++. I will try to understand you code later. Thank you for your help though.
 

BigPrince

macrumors 68020
Original poster
Dec 27, 2006
2,053
111
the control q and control d procude a ¿ and then whole program runs but does not stop at my next function like it is supposed too. newline and control-d does nothing.
 

iMeowbot

macrumors G3
Aug 30, 2003
8,634
0
the control q and control d procude a ¿
Yes, that weird character is a side effect of using weird emacish escape sequences to squeeze control characters into the stdin stream.
and then whole program runs but does not stop at my next function like it is supposed too.
okay, now I'm getting suspicious about the general use of EOF in this program, whether or not it's even the right thing to do (it may accidentally do the right thing under Windows just as a side effect).

Does the program ask for any input later on in the program? If yes, EOF isn't really what you want to use. Can you post source?

[ If you want stdin to still work after seeing an EOF, you'll need a clearerr(stdin) somewhere. ]

newline and control-d does nothing.
Correct, that won't work inside the Xcode log window.
 

BigPrince

macrumors 68020
Original poster
Dec 27, 2006
2,053
111
Here is my code. I clear the cin.

Code:
#include <iostream>                    //This is our prototyping and global 
using namespace std ;                  //declarations 

void display(int[], int) ;
int average(int, int, int[]) ;
void search(int[], int) ;
void sort(int, int, int, int, int[], int) ;
int largest(int, int[]) ;
int smallest(int[]) ;


int main()                              //This is the main function where all
{                                        //other functions are called to.
	int array[50] ;            
	int actsize = 0  ;                     //Declartion and instatiation of 
	int sum = 0 ;                        //variables
	int arrayLength = 0 ;
	int passactsize = 0 ;
	int searchIndex = 0 ;
	int minIndex = 0 ;
	int choice ;

	cout << "Please choose one of the following" << endl ;     //These are the Menu
                                                               //options
	cout << "Option 1 input from keyboard" << endl ;
	cout << "Option 2 quit " << endl ;

	cin >> choice;

	if(choice = 1)                    //If the user choses choice 1 we enter the if
	{                                 //statement
		switch(choice)
		{case 1:

	cout << "Welcome to Oliver's Number Array Program" << endl ;     //Welcoming statements
	cout << "Please follow the directions carefully" << endl ;
 
	cout << "Enter the value, press enter, and repeat as needed. When done press CTRL-Z" << endl ;
                                                         //Prompting user for keyboard input
	while(cin)                                           //Takes the input and puts into an array
	{
		cin >> array[actsize] ;
		actsize++ ;
	}
		cin.clear() ;

		actsize--;	//Subtract element so that end of file is not displayed in array.

	cout << "The Array will now be displayed to you" << endl ;       //Array will now be displayed
	
	display(array, actsize) ;             //Display array Function

	cout << "The average of the Array will now be computed" << endl ;
	cout << "The average is ";                                      //Average of array will be found

	if (actsize > 0)

		cout <<  average(sum, actsize, array) << endl ;              //Calls the average function
	
	else
		cout << "zero" << endl ;                                  //Output if there is no average


	cout << "You will now be given an opportunity to search the Array" << endl ;
	                                                       //User is told they may search the array
	search(array, actsize) ;                                 //Calls search function

	cout << "The Array will now be sorted" << endl ;      //User is told the array will be sorted
                                                          //using a selection sort from the book
	sort(arrayLength, passactsize, searchIndex, minIndex, array, actsize) ; //Calls the sort function
	
	cout << endl;                                           //Some space to make it look nicer
	cout << endl;
	cout << endl;
	cout << endl;

	cout << "The Array will now be displayed to you" << endl ;   //Tells user that Array will be displayed
	
	display(array, actsize) ;        //Calling display function

	cout << "The largest value in the array is" << endl ;     //Tells user that the largest value will be 
	                                                          //Displayed
	largest(actsize, array) ;         //Calling largest function
	
	cout << array[actsize-1] << endl ;

	cout << "The smallest value in the array is" << endl ;    //Tells user that the largest value will be
	                                                          //displayed
	smallest(array) ;                //Calling smallest function
  	
	cout << array[0] << endl ;

		}}
	else
		cout << endl;  //if choice 2, then the main is not entered
}



//Purpose: This function is used to dispaly the array to the user
//Pre: Waiting to be called to read the array
//Post: Function reads and displays the array
void display(int array[], int actsize)
{
	cout << "Index" << "   " <<  "Arrayelement" << endl ;

	for (int L = 0 ; L < actsize  ; L++)

		cout << L << "." << "            "  << array[L] << endl ;
}

//Purpose: This function calculates the average of the array
//Pre: Waiting to be called to read values from array and take average
//Post: Average of the array is calculated and sent back to the main


int average(int sum, int actsize, int array[])
{
	sum = 0 ;

	for (int L = 0 ; L < actsize ; L++)
		sum = sum + array[L] ;

	return sum / actsize ;
}


//Purpose: To give the user an opportunity to search the array
//Pre: Waiting to be called to read and compare values of the array
//Post: Array is searched and values are compared to see if the target is in the Array

void search(int array[], int actsize)
{
	float target ;
	int t = 0 ;

	cout << "Enter a number to search for ";

	cin >> target ;

	while(t < actsize && array[t] != target)
	t++ ;

	if (t != actsize)

	cout << target << "is in the array" << endl ;

	else

	cout << target << "is not in the array" << endl ;
}

//Purpose: This is a selection sort from the book to sort the array in ascending order
//Pre: Waiting to be called to sort the array in ascending order
//Post: Array is now sorted in ascending order

void sort(int arrayLength , int passactsize, int searchIndex, int minIndex, int array[], int actsize)
{
	int temp ;
	arrayLength = actsize ;

	for (passactsize = 0 ; passactsize < arrayLength ; passactsize++)
	{
		minIndex = passactsize ;

		for (searchIndex = passactsize +1 ; searchIndex < arrayLength; searchIndex++)
		{
			if (array[searchIndex] < (array[minIndex]))
				minIndex = searchIndex ;
		}
		temp = array[minIndex] ;
		array[minIndex] = array[passactsize] ;
		array[passactsize] = temp ;
	}
}

//Purpose: Find the largest value in a sorted array
//Pre: Waiting to be called to display the largest value in array
//Post: The largest value is found and sent back to main
int largest(int actsize, int array[])
{
	return array[actsize-1] ;
}


//Purpose: Find the smallest value in a sorted array
//Pre: Waiting to be called to display the smallest value in array
//Post: The smallest value is found and sent back to main
int smallest(int array[])
{
	return array[0] ;
}


This is my first C++ program doing Function Decomposition. Please be friendly, but any advice is appreciated.

//This program takes values from the keyboard and puts them in an
//Array Data Structure and maniuplates the array. It can display
//the array, the the average of the array, search the array, sort
//the array via a selection sort, and look for the largest and smallest
//value.
 

iMeowbot

macrumors G3
Aug 30, 2003
8,634
0
Starting with a new Xcode project (I chose C++ Tool in the Command Line Utility section), I pasted your code into main.cpp. I then pressed the Build and Go button, here is the transcript. the stuff I typed is in bold.

the program said:
[Session started at 2007-02-08 00:57:19 -0500.]
Please choose one of the following
Option 1 input from keyboard
Option 2 quit
1
Welcome to Oliver's Number Array Program
Please follow the directions carefully
Enter the value, press enter, and repeat as needed. When done press CTRL-Z
1
2
3
(here I typed ctrl-q ctrl-d return)


The Array will now be displayed to you
Index Arrayelement
0. 1
1. 2
2. 3
The average of the Array will now be computed
The average is 2
You will now be given an opportunity to search the Array
Enter a number to search for -2.26144e-29is not in the array
The Array will now be sorted




The Array will now be displayed to you
Index Arrayelement
0. 1
1. 2
2. 3
The largest value in the array is
3
The smallest value in the array is
1

numbers has exited with status 0.

Is this what you expected to happen? What should be different? Is it the search function that we are supposed to be looking at first here?
 

iMeowbot

macrumors G3
Aug 30, 2003
8,634
0
Okay, I found some oddness in the GCC libraries (it doesn't seem to be specific to Mac either, I turned up a bug report for djgpp with the same problem). The clear() method doesn't really reset eof.

As a workaround, you'll need to use both:

cin.clear();
clearerr(stdin);

=-=-=-=

While the above will make the program work, it's really not a good way to be doing things, it's a little too device dependent. This search for EOF and then waiting for more input will fail if stdin is an actual file, ctrl-D only has a special meaning (one that can be undone) on interactive streams. The Right Thing to do is look for a non-numeric entry (or some specific value) for the end of the number sequence.
 

BigPrince

macrumors 68020
Original poster
Dec 27, 2006
2,053
111
Okay, I found some oddness in the GCC libraries (it doesn't seem to be specific to Mac either, I turned up a bug report for djgpp with the same problem). The clear() method doesn't really reset eof.

As a workaround, you'll need to use both:

cin.clear();
clearerr(stdin);

=-=-=-=

While the above will make the program work, it's really not a good way to be doing things, it's a little too device dependent. This search for EOF and then waiting for more input will fail if stdin is an actual file, ctrl-D only has a special meaning (one that can be undone) on interactive streams. The Right Thing to do is look for a non-numeric entry (or some specific value) for the end of the number sequence.


Thank you for your time. But it does work "correctly" on a PC, at least visually. I am in the process of making it more menu you driven then just those 2 options, allowing for repeat attempts for searching for numbers. Eventually the user will be given the choice for file input or keyboard input. I just wanted to start off slow.

Oliver

PS. If you have time could you tell me what clearerr(stdin); is?
 

mbabauer

macrumors regular
Feb 14, 2006
105
0
I still say the best idea is to look for an empty line and use that to end the list. Relying on control characters is not really a safe cross platform approach. You should invest some time to check out the sample code that toddburch presented...its a safe alternative.

Keep in mind that when you are debugging code in XCode, its not really running under the same circumstances as it would outside of XCode. Dubuggers have a sneaky way of intercepting certain inputs.
 

iMeowbot

macrumors G3
Aug 30, 2003
8,634
0
PS. If you have time could you tell me what clearerr(stdin); is?

It's really part of the C stdio library, that is, stdio's version of std:cin.clear(). GNU's istream happens to be implemented on top of stdio, that's why this workaround works.

The C++ standard likes to pretend that operating systems don't exist, so that questions like how EOF is treated (can it be undone or not in some cases) are glossed over. So, depending on who you ask, it's a stupid bug or not a bug at all :eek:
 

BigPrince

macrumors 68020
Original poster
Dec 27, 2006
2,053
111
I still say the best idea is to look for an empty line and use that to end the list. Relying on control characters is not really a safe cross platform approach. You should invest some time to check out the sample code that toddburch presented...its a safe alternative.

Keep in mind that when you are debugging code in XCode, its not really running under the same circumstances as it would outside of XCode. Dubuggers have a sneaky way of intercepting certain inputs.

Ya, cross platforming is not my biggest concern right now, just would rather code from home then school using visual studio. But its good to know for the future.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.