Thread: EOF isnt working....

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    20

    EOF isnt working....

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int c, d;
        
        while((c = getchar() - '0') != EOF)  {
             if(c == 2)
               printf("fool\n");   /* once again, my pointless messages */
             if(c == 1)
                printf("hehe\n");
                }
    }
    U r suppose to press F6 to end this program right? but it wont terminate for some reason...

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    2,108
    You would need your while condition to be
    "while((c = getchar() - '0') != EOF-'0')

    Because you are subtracting '0' from getchar() before testing for EOF.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. doesn't stop before EOF
    By zmaker5 in forum C Programming
    Replies: 15
    Last Post: 08-03-2007, 02:25 PM
  2. Check for EOF when using fgets
    By daghenningsorbo in forum C Programming
    Replies: 6
    Last Post: 05-16-2007, 06:48 PM
  3. EOF messing up my input stream?
    By Decrypt in forum C++ Programming
    Replies: 4
    Last Post: 09-30-2005, 03:00 PM
  4. Replies: 2
    Last Post: 11-10-2003, 08:12 PM
  5. files won't stop being read!!!
    By jverkoey in forum C++ Programming
    Replies: 15
    Last Post: 04-10-2003, 05:28 AM