fflush(stdin)

This is a discussion on fflush(stdin) within the C Programming forums, part of the General Programming Boards category; can someone please explain why someone would put fflush(stdin) after every user input? I looked it up in my book ...

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    37

    fflush(stdin)

    can someone please explain why someone would put fflush(stdin) after every user input? I looked it up in my book but that made absolutley no sense.

  2. #2
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,825
    Because there's still a newline character in the input stream sometimes, and that can screw up functions like scanf.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    This is undefined behavior. Basicly it's "supposed to" clear the input stream of all pending input, thus making it clear for you to begin reading again without having junk input.

    This will work in MSVC++, because according to the msdn, their implementation of fflush will clear any stream passed to it.

    However, this will not work on all compilers, because it is not a defined behaviour. This means that the ANSI standard commitee haven't specified what should happen when you flush an input stream.

    As such, it's up to each compiler vendor to decide what happend. In some cases it works, in some it doesn't.

    In either case, there are better ways to do it, search the board and you'll find plenty on it.

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

  4. #4
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,825
    hey quzah - I know that's undefined, but why? I mean, fflush(stdout) is defined, correct? It just seems to me like having fflush(stdin) do the same thing on the input stream would make sense... Or am I about to get smacked for my ignorance?

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I'm not sure why it's undefined. I just know that it is. The msdn states that their version will flush a file stream. Period. Meaning, in their version of it, any stream passed to it will be flushed correctly.

    However, I also have tried it on another compiler and had it not work. (Probably gcc, I don't recall off hand.)

    IIRC, you can use "rewind(stdin)" to get the desired results. I don't recall off hand exactly, but I believe that works, and I believe it's considered "safe" or "defined behavior". I may be wrong here tho.

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

  6. #6
    Registered User
    Join Date
    Jul 2002
    Posts
    37

    hmm

    ok I somewhat understand whats going on..

    BUT...

    In a previous post I had some issues when sending numbers with 2 decimal points into an array. (ie 9.9.84) 9.9 would go into the first element and 0.84 would automatically go into the second. When I put fflush(stdin) after the scanf() line, not it omits the .84. WHICH IS WHAT I WANT IT TO DO, YEAHHHH...

    but why.. Im not understanding what afflush does and what (stdin) is exactly doing either.

    Sorry for being a pain... just curious.

    R

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It's as I described. It is in your case removing any pending input. Thus, your scanf call snips off the 9.9, leaving the remainder in the input stream / buffer. You next call fflush(stdin) (which you should actually use), and it clears the buffer.

    What's not to understand?

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

  8. #8
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,825
    > WHICH IS WHAT I WANT IT TO DO, YEAHHHH...

    So it works with your compiler - that's nice. The poitn quzah was making is that it might not work on other compilers, since it's not standard.

    > (stdin)

    It's just the standard input file stream...

  9. #9
    Registered User
    Join Date
    Jul 2002
    Posts
    37

    ahh

    I understand now.... you guys rock!

    Just wait until you see what my next question is going to be tomorrow.. . muhahah


  10. #10
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231

    Re: ahh

    Originally posted by RyeDunn
    Just wait until you see what my next question is going to be tomorrow.. . muhahah
    Let me take a guess....

    - why not void main()
    or
    - whats wrong with gets()
    or
    - why won't my C compile work with cout.
    or
    - how do you get a random number between 1 and 10.

    am I close?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  11. #11
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    523
    I'll put my money on

    -How to clear the screen
    Last edited by C_Coder; 07-18-2002 at 03:04 PM.
    All spelling mistakes, syntatical errors and stupid comments are intentional.

  12. #12
    Registered User
    Join Date
    Jul 2002
    Posts
    37

    ouch

    tough crowd.

  13. #13
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    I bet 2 cents on "How do I reverse a string?"
    Haha, you probably figure out by now that I love those posts.

  14. #14
    Registered User
    Join Date
    Jul 2002
    Posts
    37

    1000

    Hammer,
    Im honored to be the one you decided to slam for your 1000th post. Congrats.

    Rye

  15. #15
    Registered User
    Join Date
    Feb 2002
    Posts
    591
    What about random numbers?? Please Please I haven't answeared one of those question in ...well days now.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 02:01 AM
  2. fread/fwrite
    By chopficaro in forum C Programming
    Replies: 6
    Last Post: 05-11-2008, 02:48 AM
  3. Searching Into Files
    By St0rM-MaN in forum C Programming
    Replies: 12
    Last Post: 04-26-2007, 10:02 AM
  4. Newbie Question - fflush(stdin) & fpurge(stdin) on Mac and PC
    By tvsinesperanto in forum C Programming
    Replies: 34
    Last Post: 03-11-2006, 12:13 PM
  5. Linked List Need Help Urgent
    By ykchua in forum C Programming
    Replies: 5
    Last Post: 08-17-2004, 03:57 PM

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