Like Tree3Likes
  • 1 Post By std10093
  • 1 Post By dan108
  • 1 Post By vart

Code after while loop not being executed

This is a discussion on Code after while loop not being executed within the C Programming forums, part of the General Programming Boards category; Hi, this is probably a very novice question but I can't figure out why this isn't working and searching the ...

  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
    language hopper dennis.cpp's Avatar
    Join Date
    Jan 2011
    Location
    Herborn, Hesse, Germany
    Posts
    197
    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?
    Hardware: Intel® Core™ i7-3630QM CPU @ 2.40GHz × 8
    Operating system: Debian Testing/Jessie / Linux 3.16-2-amd64 / KDE SC 4.14.1
    Compiler: gcc 4.9.1-16 with KDevelop 4.7.0

  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.
    dennis.cpp likes this.
    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 !
    dennis.cpp likes this.

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

    myprog < sample.txt
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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.
    dennis.cpp likes this.
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21