Thread: Code after while loop not being executed

  1. #1
    Registered User
    Join Date
    Sep 2013
    Posts
    4

    Code after while loop not being executed

    Hi, this is probably a very novice question but I can't figure out why this isn't working and searching the web and this forum hasn't helped either.

    I have this:

    Code:
    #include <stdio.h>
    
    
    main()
    {
        int c, nl;
    
    
        nl = 0;
        while((c = getchar()) != EOF) {
            if (c == '\n')
                ++nl;
        }
        
        printf("%d\n",nl);
    }
    It counts the occurrences of new lines. However, printf() is never run after the while loop has terminated. Why is this?

  2. #2
    spaghetticode
    Guest
    Is this an exercise from the K&R book? I think I remember this one of the typical exercises... :-)

    Program looks okay, what input are you giving it and how do you end execution? Sure that it's not just still running?

  3. #3
    Registered User
    Join Date
    Sep 2013
    Posts
    4
    Yes indeed it is
    I'm just typing in input strings like "test bla bla" without the quotes and ending by pressing enter.
    Having said that, I'm not actually ending it by just pressing enter am I? I have to end it another way right?

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    You are correct both of you. dan welcome to the forum.

    You have to input the signal of EOF, which in UNiX is ctrl+d, if I remember correct.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  5. #5
    Registered User
    Join Date
    Sep 2013
    Posts
    4
    Sound familiar. I'm on windows at the moment though. Do you happen to know the command for it there?
    Edit: Found it. Ctrl+Z !

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Or simply take a text file and pass it to your programs stdin

    myprog < sample.txt
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    I was thinking of pointing what vart said. Recall that EOF stands for End Of File, thus in your file, you do not need anything like ctrl + d, ctrl + z, or to write yourself EOF at the end of the file. The system tracks EOF automatically, when it comes to files.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  8. #8
    Registered User
    Join Date
    Sep 2013
    Posts
    4
    I realized I could do that, but I'm following the book and it covers reading from files a bit later on. I wanted to try this method first.
    Thanks for the help and tips everyone

  9. #9
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by dan108 View Post
    I realized I could do that, but I'm following the book and it covers reading from files a bit later on. I wanted to try this method first.
    Thanks for the help and tips everyone
    it is not "reading from file" you are referencing in the book. It is still reading from standard input. Just that standard input is redirected from keyboard to file.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extra loop being executed in do-while loop.
    By Krabiki in forum C Programming
    Replies: 9
    Last Post: 07-03-2013, 10:17 PM
  2. Function is not executed....
    By enakta13 in forum C Programming
    Replies: 4
    Last Post: 09-25-2012, 09:57 PM
  3. Replies: 4
    Last Post: 08-29-2012, 06:15 PM
  4. why a certain line is not being executed
    By transgalactic2 in forum C Programming
    Replies: 19
    Last Post: 10-10-2008, 11:35 AM