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.
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.
Because there's still a newline character in the input stream sometimes, and that can screw up functions like scanf.
-Govtcheez
[email protected]
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.
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?
-Govtcheez
[email protected]
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.
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
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.
> 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...
-Govtcheez
[email protected]
I understand now.... you guys rock!
Just wait until you see what my next question is going to be tomorrow.. . muhahah
Let me take a guess....Originally posted by RyeDunn
Just wait until you see what my next question is going to be tomorrow.. . muhahah
- 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]
I'll put my money on
-How to clear the screen
Last edited by C_Coder; 07-18-2002 at 02:04 PM.
All spelling mistakes, syntatical errors and stupid comments are intentional.
tough crowd.
I bet 2 cents on "How do I reverse a string?"
Haha, you probably figure out by now that I love those posts.
Hammer,
Im honored to be the one you decided to slam for your 1000th post. Congrats.
Rye
What about random numbers?? Please Please I haven't answeared one of those question in ...well days now.