Thread: EOF end input

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    63

    EOF end input

    Original assignment: "A user should be able to enter up to 200 items of float data. The program should calculate the number of items in the data. The symbol <EOF> in the sample run below should be replaced with CTRL-Z or CTRL-D or the end-of-file symbol on your system."

    Example:

    Item #1 : 25
    Item #2 : 36
    Item #3 : 27.5
    Item #4 : 28
    Item #5 : 32
    Item #6 : 33.25
    Item #7 : <EOF>


    I'm having trouble implementing the CTRL-Z in my program, any ideas?

    Code:
    /*
    
    My name is Jack Trocinski
    
    */
    
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
    	float array[200]; // array of type float with 200 elements
    	int i; // used in for loop
    	
    	for ( i = 0; i < 200; ++i) {
    		printf("Please input your data and press enter, use CTRL-Z to when you're done: ");
    		scanf("%f", &array[i]);
    		
    		if (array[i] = EOF) break;
    	}
    	
    	--i;
    	
    	printf("The number items you have entered is: %d", i);
    	
    	system("pause");
    	return 0;
    }
    Last edited by yacek; 12-09-2010 at 10:57 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Notice that scanf doesn't put EOF in the variable -- it returns EOF. When EOF happens, it leaves the variable alone.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    In this line:

    Code:
    if (array[i] = EOF) break;
    You are assigning the EOF to array[i].

    This if statement will always be true.

    Jim
    Last edited by jimblumberg; 12-09-2010 at 11:10 PM.

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    63
    Hmm, I tried doing that. If I put in CTRL-Z the program doesn't react or rather it takes that as an input and then continues the loop and if I use CTRL-D it just does the for loop 200 times without asking me for any input.

    updated code

    Code:
    /*
    
    My name is Jack Trocinski
    
    */
    
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
    	float array[200]; // array of type float with 200 elements
    	int i; // used in for loop
    	
    	for ( i = 0; i < 200; ++i) {
    		
    		printf("Please input your data and press enter, use CTRL-Z when you're done: ");
    		scanf("%f", &array[i]);
    		
    		if (array[i] == EOF) break;
    	}
    	
    	printf("The number items you have entered is: %d", i);
    	
    	system("pause");
    	return 0;
    }

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    EOF is defined in stdio.h, and is frequently -1.


    Look in your include folder for stdio.h, and then for a line like this:
    Code:
    #define EOF	(-1)			/* End of file indicator */
    Be careful not to change anything in that file - and if you do change something accidentally, be SURE not to save it.

  6. #6
    Registered User
    Join Date
    Oct 2010
    Posts
    63
    Yes Adak, when I input a value of -1 the program runs fine. The question is then how do use CTRL-Z to break the loop because what if the user wants to input -1 as one of their values in the data.

  7. #7
    Registered User
    Join Date
    Oct 2010
    Posts
    63
    Any suggestions, on how to use CTRL-Z to end the input?

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Your assignment says that to end the loop early, the user will use the EOF value - that is typically -1 in C. Not CTRL+z.

    In Windows, it's CRTL+z, but this is not Windows, and the assignment doesn't ask the user to enter CRTL+z.

    When you make your file up, you don't need to enter CTRL+z, unless you copy it directly from the console:
    Code:
    C:\> copy con test.txt <enter>
    This is a test<CTRL+z>
    
      1 file copied
    C:\>
    The CTRL+z is put into the file, by the program creating the file (Notepad, etc.). In C, it's put in when you close the file, automatically. You never have to add this or -1, to the end of a file in C.

    In R/L, if you expected the user to enter -1 as data, you just don't use that as your sentinel value to break out of the enter data loop. Maybe -99 or -9999, or INT_MAX or INT_MIN, etc.

    Note that the file won't have a -1 at the end of it. You can check that with a Hex Editor. -1 is EOF for C - a logical construct.
    Last edited by Adak; 12-10-2010 at 12:04 AM.

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by yacek View Post
    Any suggestions, on how to use CTRL-Z to end the input?
    Clearly you have not thought about the answers given all that much. tabstop answered this question in the second post.

    Check the value returned by scanf(), not the value of array[i].
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  10. #10
    Registered User
    Join Date
    Oct 2010
    Posts
    63
    I've been thinking about this all day Grumpy. Still no idea how to get CTRL-Z to work.
    Last edited by yacek; 12-10-2010 at 01:20 AM.

  11. #11
    Registered User
    Join Date
    Oct 2010
    Posts
    63
    I guess it would return a 0. Nice, I got it to work!
    Last edited by yacek; 12-10-2010 at 01:26 AM.

  12. #12
    Registered User
    Join Date
    Oct 2010
    Posts
    63
    Hmm i thought I had it but it's still not working, doesn't break out of the loop with CTRL-Z. updated code, I added a menu since I'm going to be adding on functions later:

    Code:
    /*
    
    My name is Jack Trocinski
    
    */
    
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
    	float array[200]; // array of type float with 200 elements
    	int i; // used in for loop
    	int j; // used in menu	
    	
    	do {
    		printf("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n");
    		printf("@                              @\n");
    		printf("@ 1. Enter data                @\n");
    		printf("@ 2. Display data & statistics @\n");
    		printf("@ 3. Exit                      @\n");
    		printf("@                              @\n");
    		printf("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n\n");
    		
    		scanf("%d", &j);
    
    			if (j == 1) {
    				for ( i = 0; i < 200; ++i) {
    				printf("Please input your data and press enter, use CTRL-Z to when you're done: ");
    				scanf("%f", &array[i]);				
    				
    				if ( array[i] != 1  ) break;
    				}
    			}
    			
    	} while (j != 3);
    
    	return 0;
    }
    Last edited by yacek; 12-10-2010 at 02:10 AM.

  13. #13
    Registered User
    Join Date
    Oct 2010
    Posts
    63
    finally got it to work with this statement

    Code:
    if ( scanf("%f", &array[i]) != 1 ) break;

  14. #14
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Hoorah! That is what tabstop told you to do in the second post of this thread.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  15. #15
    Registered User
    Join Date
    Oct 2010
    Posts
    63
    Thanks, you don't know how long that took me to figure out!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. input array with EOF
    By aweida in forum C Programming
    Replies: 2
    Last Post: 11-01-2010, 05:41 AM
  2. Can you help me about tolower() in file
    By nctar in forum C Programming
    Replies: 7
    Last Post: 05-12-2010, 10:04 AM
  3. Syntax error at end of input
    By implor in forum C Programming
    Replies: 4
    Last Post: 06-16-2009, 09:39 AM
  4. need some help with last part of arrays
    By Lince in forum C Programming
    Replies: 3
    Last Post: 11-18-2006, 09:13 AM
  5. end of standard input
    By alphaoide in forum C++ Programming
    Replies: 5
    Last Post: 06-21-2004, 08:43 PM