End of File

Discussion in 'Mac Programming' started by BigPrince, Feb 5, 2007.

  1. macrumors 68020

    Joined:
    Dec 27, 2006
    #1
    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?
     
  2. Moderator emeritus

    Joined:
    Jun 25, 2002
    Location:
    Gone but not forgotten.
    #2
    Control-D should work on any UNIX machine for end of file.

    By the way, it's Xcode. ;)
     
  3. thread starter macrumors 68020

    Joined:
    Dec 27, 2006
    #3
    yaya, i keep screwing up the name, sue me.

    thx for the help though


    edit:Control-D would not do it
     
  4. macrumors 6502

    Joined:
    Jul 25, 2006
    #4
    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...
     
  5. thread starter macrumors 68020

    Joined:
    Dec 27, 2006
    #5
    ok so how can the user tell the program they are done inputting their values from the keyboard?
     
  6. Guest

    caveman_uk

    Joined:
    Feb 17, 2003
    Location:
    Hitchin, Herts, UK
    #6
    How about if the just enter return (with no number) to end the list?
     
  7. thread starter macrumors 68020

    Joined:
    Dec 27, 2006
    #7
    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.
     
  8. macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #8
    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;
    }
    
     
  9. thread starter macrumors 68020

    Joined:
    Dec 27, 2006
    #9
    I am just starting out in C++. I will try to understand you code later. Thank you for your help though.
     
  10. macrumors G3

    iMeowbot

    Joined:
    Aug 30, 2003
    #10
    To get an EOF in the Xcode run log, type ctrl-Q ctrl-D and return.
     
  11. thread starter macrumors 68020

    Joined:
    Dec 27, 2006
    #11
    thx, i will try this when i get home
     
  12. macrumors 68000

    savar

    Joined:
    Jun 6, 2003
    Location:
    District of Columbia
    #12
    Newline followed by control D represents EOF on Unix.
     
  13. thread starter macrumors 68020

    Joined:
    Dec 27, 2006
    #13
    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.
     
  14. macrumors G3

    iMeowbot

    Joined:
    Aug 30, 2003
    #14
    Yes, that weird character is a side effect of using weird emacish escape sequences to squeeze control characters into the stdin stream.
    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. ]

    Correct, that won't work inside the Xcode log window.
     
  15. thread starter macrumors 68020

    Joined:
    Dec 27, 2006
    #15
    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.
     
  16. macrumors G3

    iMeowbot

    Joined:
    Aug 30, 2003
    #16
    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.

    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?
     
  17. macrumors G3

    iMeowbot

    Joined:
    Aug 30, 2003
    #17
    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.
     
  18. thread starter macrumors 68020

    Joined:
    Dec 27, 2006
    #18

    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?
     
  19. macrumors regular

    Joined:
    Feb 14, 2006
    #19
    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.
     
  20. macrumors G3

    iMeowbot

    Joined:
    Aug 30, 2003
    #20
    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:
     
  21. thread starter macrumors 68020

    Joined:
    Dec 27, 2006
    #21
    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.
     

Share This Page