Thread: fflush not working

  1. #1
    Registered User
    Join Date
    Mar 2006
    Location
    Bangalore, INDIA
    Posts
    43

    fflush not working

    Code:
    main()
    {
             char a,b; 
             printf("Enter a value\n");
             scanf("%c", &a);                // the value entered is X and so in buffer 
                                                       // its saves as X\n
             fflush(stdin);                      // this is not flushing keyboard buffer
             scanf("%c",&b);                 // b takes the value \n
    }
    so for the above how shall i program so that the value of "b" is taken by user. and wish to hear why fflush(stdin) is not flushing the keyboard buffer

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    You cannot fflush stdin. This is covered in the FAQ.

    EDIT: More information can be found here

  3. #3
    Registered User
    Join Date
    Mar 2006
    Location
    Bangalore, INDIA
    Posts
    43
    Yes, i know that fflush is used for output stream, in that case i dont know what to do with the input stream....for exampl....when the user is asked to enter a character user enters "x" so in buffer the value is stored at "x\n"...where the value "x" is stored in variable "a" and during second scanf the value "\n" is stored in variable "b" instead of taking from the user

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Well you read the FAQ, learn how marvellous fgets() is, and then forget about using scanf() ever again for trying to read input.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Mar 2006
    Location
    Bangalore, INDIA
    Posts
    43
    I agree with u that fgets is very much better than scanf....but need to understand why scanf fails there and if at all we are in need to use scanf itself then how to solve the above problem

  6. #6
    Registered User
    Join Date
    Mar 2006
    Location
    Bangalore, INDIA
    Posts
    43
    ok now the fflush(stdin) behaviour is undefined....then is there any method by which we are flush the input stream.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Yes, read the FAQ!


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Mar 2006
    Location
    Bangalore, INDIA
    Posts
    43
    the below was the solution given in FAQ.

    "check your compiler's documentation to see if it has a (non-portable) method for flushing input. "

    I'm using linux cc compiler to compile my c programs what shall i use to flush input stream

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    You use fgets() and the whole problem goes away.
    Are you listening?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    READ THE FAQ! clicky click!


    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    41
    Quote Originally Posted by quzah
    READ THE FAQ! clicky click!


    Quzah.
    lol are you a teacher?

  12. #12
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    I'd pity the student who would have Quzah as a teacher.

    Although at least they'd learn something.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function not working
    By sloopy in forum C Programming
    Replies: 31
    Last Post: 11-12-2005, 08:08 PM
  2. Program Not working Right
    By raven420smoke in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2005, 03:21 AM
  3. Trying to eject D drive using code, but not working... :(
    By snowfrog in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2005, 07:47 PM
  4. x on upper right corner not working
    By caduardo21 in forum Windows Programming
    Replies: 1
    Last Post: 02-20-2005, 08:35 PM
  5. cygwin -> unix , my code not working properly ;(
    By CyC|OpS in forum C Programming
    Replies: 4
    Last Post: 05-18-2002, 04:08 AM