Thread: Clearing the input stream

  1. #1
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321

    Question Clearing the input stream

    Someone told me to use this to clear input buffer
    Code:
    while((ch=getchar())!='\n' && ch!=EOF);
    ...and now this is giving me a warning:
    Code:
    #include<stdio.h>
    int main()
    {
       char a,b,ch;
       printf("\nEnter a character:");
       a=getchar();
       while((ch=getchar())!='\n' && ch!=EOF);   
       printf("\nEnter another one:");
       b=getchar();
       while((ch=getchar())!='\n' && ch!=EOF);
       printf("You entered %c and %c\n",a,b);
       return 0;
    }
    13:3: warning: no newline at end of file

    The program works as expected, though.

  2. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    The warning has nothing to do with the code you added, it simply says the compiler waits for a new line at the end of your source file (after the last }).

  3. #3
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321
    Why should the compiler wait for new line after the closing brace?

    ALL the programs I write end like this
    Code:
         /****************/
         /****************/
         return 0;
    }
    But the compiler never waits for a new line there.
    Last edited by abh!shek; 05-18-2008 at 06:32 AM.

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    Why should the compiler wait for new line after the closing brace?
    The C standard requires the source files to end by a new line character, that's all. In your case, it's after the }, I didn't said it always has to be after the last }.

  5. #5
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321
    What should I change/add in this source to get rid of the warning...? You know...I don't like getting compiler warnings.

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    Open the source file in your favorite editor, move the cursor to the very end and press enter, save and recompile.

  7. #7
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321
    Thanks that works

    The C standard requires the source files to end by a new line character, that's all.
    Can you give me a link to this reference?

  8. #8
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    Section 5.1.1.2 of the standard (http://www.open-std.org/jtc1/sc22/wg...docs/n1124.pdf)

  9. #9
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Is that requirement in the C++ standard too?
    Someone told me some compilers require it, so I've always done it because I thought it was a compiler bug.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Is that requirement in the C++ standard too?
    Someone told me some compilers require it, so I've always done it because I thought it was a compiler bug.
    Yes, the C++ Standard requires that as well.
    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

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    On a different line: why does it require it?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to declare a global input stream
    By Chazij in forum C++ Programming
    Replies: 2
    Last Post: 11-18-2008, 04:53 PM
  2. Strange bug with Input stream
    By Chazij in forum C++ Programming
    Replies: 12
    Last Post: 11-18-2008, 04:52 PM
  3. reading from input stream
    By Micko in forum C++ Programming
    Replies: 8
    Last Post: 05-01-2005, 05:50 PM
  4. Clearing the Input buffer
    By Brain Cell in forum C Programming
    Replies: 5
    Last Post: 03-21-2004, 12:08 PM
  5. text input buffer clearing
    By red_Marvin in forum C++ Programming
    Replies: 4
    Last Post: 03-20-2003, 03:17 PM