Thread: Question on getchar to flush scanf

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    1

    Question on getchar to flush scanf

    Consider the following code where the function flush containing getchar is being used to flush scanf when an attempted read from the terminal was unsuccessful

    Code:
    #include <stdio.h>
     
    void flush (void) {
                int c;
                while(c=getchar()!= '\n') {
                            printf("Character read %d \n",c);
                            }
    }
     
    int main() {
                int success;
                float a;
                success=0;
                while(success==0) {
                            printf("Enter numeric value for a: ");
                            success=scanf("%f",&a);
                            flush();
                }
    return 0;
    }
    My question deals with the following behavior when values other than the expected floating point value is entered after the prompt during execution.

    1.) When you type the word "test" after the prompt during execution you will see the expected output suggesting the four characters in test where found in the buffer before the new line.

    2.) When you type the word "eek" at the prompt, you will see only two characters are found (at least using my compiler I see that)

    3.) When you type the word "nine" at the prompt, no indication is given that any of the characters in the word nine are found.

    It seems that the letters e and n (and their location in the string) are causing the problem. I was wondering if anyone could shed any light on this behavior. Thanks and apologies in advance if I have overlooked something very obvious.

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Try this.
    Code:
    #include <stdio.h>
     
    void flush(void) {
        int c;
        while((c=getchar())!= '\n' && c != EOF) /* Note parentheses around c=getchar() */
            printf("Character read: %c\n",c);
    }
     
    int main(void) {
        int success = 0;
        float a;
        while (success != 1) {
            printf("Enter numeric value for a: ");
            success = scanf("%f",&a); /* success can be either 1, 0, or EOF (a negative value, usually -1).
            flush();
        }
        return 0;
    }
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getchar and scanf
    By begin in forum C Programming
    Replies: 9
    Last Post: 06-20-2010, 07:51 PM
  2. getchar won't take any input after scanf
    By pshirishreddy in forum C Programming
    Replies: 2
    Last Post: 08-02-2009, 11:46 AM
  3. scanf, getchar, gets, etc. are ignored.
    By daltore in forum C Programming
    Replies: 19
    Last Post: 12-27-2007, 10:47 PM
  4. scanf and getchar
    By axe in forum C Programming
    Replies: 7
    Last Post: 01-11-2006, 04:45 AM
  5. my program skips scanf and getchar()
    By jk81 in forum C Programming
    Replies: 15
    Last Post: 11-29-2002, 05:54 PM