Thread: Clearing the Input buffer

  1. #1
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472

    Question Clearing the Input buffer

    I've already read the FAQ entery wich is located here so please don't mention it and focus on answering my question.

    Code:
    #include<stdio.h>
    
    int main()
    {
    int number;
    char ch , string[100];
    
         printf("Input a number : \n");
         scanf("%d", &number);
           
         while( ch = getchar() != '\n' && ch != EOF);  
    
         printf("Input a string : \n");
         fgets(string, sizeof(string), stdin);
    
         printf("You've entered %d and %s\n", number , string);
    
    return 0;
    }
    now my questions are :
    [list=1][*]Why doesn't the program wait for the user to input a character with getchar() in the line :
    while( ch = getchar() != '\n' && ch != EOF);
    [*] I've read on some sites that scanf() leaves the '\n' on the input buffer. If thats right , then why doesn't fgets() leaves one too??
    [*]why doesn't fgets() wait for input if the '\n' was in stdin??[/list=1]


    thanks for your time.
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

  2. #2
    ~viaxd() viaxd's Avatar
    Join Date
    Aug 2003
    Posts
    246
    1) ch should be int to be big enough to hold EOF.
    2) fgets puts \n at the end of the string, before \0.
    3) fgets reads a line. that is everything before \n.
    Last edited by viaxd; 03-21-2004 at 05:27 AM.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Building on previous answers:

    >Why doesn't the program wait for the user to input a character with getchar()
    Because != binds more tightly than assignment in the precedence table. What your code is doing is taking the character returned by getchar and testing it against '\n', then assigning the result of that boolean expression to ch. Because EOF is a negative quantity, a boolean value (0 or 1) will never match it. The code should instead be:
    Code:
    while ( ( ch = getchar() ) != '\n' && c != EOF )
      ;
    >I've read on some sites that scanf() leaves the '\n' on the input buffer. If thats right , then why doesn't fgets() leaves one too??
    Because fgets reads the newline and stores it in the buffer that you pass to it if the input is smaller than the buffer and no errors occur. That's why you'll see most code using fgets remove a newline immediately after:
    Code:
    char *newline = strrchr ( buffer, '\n' );
    if ( newline != NULL )
      *newline = '\0';
    >why doesn't fgets() wait for input if the '\n' was in stdin??
    Because encountering a newline is one of fgets' termination conditions.
    My best code is written with the delete key.

  4. #4
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472
    That was a great Prelude thanks alot. Thank you too Viaxd
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

  5. #5
    Registered User Zeeshan's Avatar
    Join Date
    Oct 2001
    Location
    London, United Kingdom
    Posts
    226
    One easy function to use is

    flushall();

    maybe from stdlib.h

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >flushall();
    Not standard. fflush ( NULL ) has the effect of flushing all open output streams, but if you were referring to flushing input streams, there is no standard function that does it for you.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. can someone help me with these errors please code included
    By geekrockergal in forum C Programming
    Replies: 7
    Last Post: 02-10-2009, 02:20 PM
  3. Clearing the input stream
    By abh!shek in forum C Programming
    Replies: 10
    Last Post: 05-18-2008, 03:50 PM
  4. How to put a Char into the Input buffer of a console?
    By Aidman in forum C++ Programming
    Replies: 10
    Last Post: 03-09-2003, 10:05 AM