Thread: End of file

  1. #1
    Registered User
    Join Date
    Sep 2014
    Posts
    83

    End of file

    Hi I'm using Linux.
    I have the following code:
    Code:
        c = getchar();
        no_rows = 0;
        total = 0;
        while(c != EOF)
    {....}
    in my main function, and it doesnt work when I press ctrl-d in the terminal window to stop the loop.

    If I change this to:
    Code:
        no_rows = 0;
        total = 0;
        while((c = getchar()) != EOF)
        {....}
    it works. Strange. I thought they would do the same thing. Anyone has an answer to this?

    Thanks!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    In your first code snippet, did you call getchar from within the loop?

    Also, consider:
    Code:
    int x = 123;
    while (x != -1)
    {
        /* do nothing in the loop body */
    }
    when will the loop above terminate?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2014
    Posts
    83
    I call getchar in an inner loop.
    Here's the full program:

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int no_in_row, total, no_rows, c;
        float average;
    
        
        
        no_rows = 0;
        total = 0;
        while((c = getchar()) != EOF)
        {
        
            no_in_row = 0;
            while(c != '\n')
            {
                c = getchar();
                if(c != ' ')
                    no_in_row++;
            } // Inner loop
            no_rows++;
            total = total + no_in_row;
        } // Outer loop
        
        printf("Total: %d, no_rows: %d\n", total, no_rows);
        //printf("No in row %d\n", no_in_row);
        average = (float)total / no_rows;
        printf("Average characters in a row: %.2f\n", average);
        return 0;
    }

  4. #4
    Registered User
    Join Date
    Sep 2014
    Posts
    83
    Oh now I got it. I added a c = getchar() inside the outer loop too. Thanks a lot again Laserlight!!!

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    In that case, it should have worked, but you should have that check for EOF in the inner loop too.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Sep 2014
    Posts
    83
    Yep I did that after.

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int no_in_row, total, no_rows, c;
        float average;
    
        
        c = getchar();
        no_rows = 0;
        total = 0;
        while(c != EOF)
        {
        
            no_in_row = 0;
            while((c != '\n') && (c != EOF))
            {
                if(c != ' ')
                    no_in_row++;
                
                c = getchar();
            } // Inner loop
            no_rows++;
            total = total + no_in_row;
        
            c = getchar();
        } // Outer loop
        
        average = (float)total / no_rows;
        
        printf("\nTotal: %d, no_rows: %d\n", total, no_rows);
        printf("Average characters in a row: %.2f\n", average);
        
        
        //printf("No in row %d\n", no_in_row);
        
        
        
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 08-30-2014, 11:07 PM
  2. Replies: 16
    Last Post: 04-05-2014, 09:23 PM
  3. Replies: 3
    Last Post: 11-28-2012, 09:16 AM
  4. Replies: 11
    Last Post: 09-25-2011, 12:22 AM
  5. Replies: 4
    Last Post: 07-06-2006, 02:53 AM