well, i think i am going to write a small tutorials including all the
frequently asked questions on this board... really every one!
well, i think i am going to write a small tutorials including all the
frequently asked questions on this board... really every one!
Hi Rye, I wasn't meaning to slam you, only have a little fun No hard feelings, ehHammer,
Im honored to be the one you decided to slam for your 1000th post. Congrats.
Rye
And dammit, I didn't even notice my 1000th post mark, and I was trying to remember it too
Ask us any question you like... they're all fun to answer, even ones that get asked too many times! (as you can tell by this thread )
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
no offense taken... I know its all in fun and some of the questions I do ask are in the book/FAQs but sometime I have difficulty understanding or implementing it in my specific program.
Speaking for us n00bs... we really do appreciate all the time and help you guys provide to us people who have noone else to ask.
>can someone please explain why someone would put fflush(stdin) after every user input?
fflush ( stdin ) is undefined, don't use it. But to answer your question, there's no need to flush the input stream after every call. The problem is with some functions which leave unread data in the stream, this unread data will cause problems with the next call for input since whatever is in the buffer must be processed before anymore user input will be seen.
>I'm not sure why it's undefined.
Excuse me while I have a heart attack. You should know this quzah.
Aside from the fact that the ISO C standard specifies that if the stream is not an output or update stream the behavior is undefined, you have to consider that the definition of fflush is to write unwritten data from the stream, not discard unread data which is a completely different meaning and function.
>Or am I about to get smacked for my ignorance?
That's about the size of it, input streams and output streams are handled differently. Having one function manage both the flushing of output and the discarding of input is silly since the two operations are wildly different. At best this would require two flush functions, one for input and one for output. Also consider that not all input systems are cooked by default, what would happen if you tried to use an input buffer flush on a raw input stream with no buffer? That seems like a prime candidate for undefined behavior to me.
-Prelude
My best code is written with the delete key.
I might be beating a dead horse on this but:
>fflush ( stdin ) is undefined, don't use it.
Could you recommend something else that would be better?
You helped me previously when I asked about only allowing particular integers and you were very helpful by posting thisCode:// GATHER DATA for (e = 0; e < maxnum; e++) { printf("Enter number %d : ", e + 1); scanf("%lf", &a[e]); fflush(stdin); }
But the only problem with that is that I can still enter multiple (. and - characters) So I started playing with printf instead of cout and it looked likeCode:bool validate ( std::string val ) { int i, j; const std::string valid = "0123456789-."; for ( i = 0; i < val.length(); i++ ) { for ( j = 0; j < valid.length(); j++ ) if ( val[i] == valid[j] ) break; if ( j == valid.length() ) return false; } return true; }
was a better alternative.Code:scanf("%lf", &a[e]); printf("\n %g", a[e]);
It just seems that I keep taking 1 step forward and 3 steps back!
Try the following as an alternative to fflush(stdin).
Code:void clearInput() { char junk[81]; fgets(junk , 80, stdin); }
>Could you recommend something else that would be better?
I prefer some variation of
while ( getchar() != '\n' );
>But the only problem with that is that I can still enter multiple (. and - characters)
Of course, my function was just a basic validation to get you on your way. It's up to you to handle the required special cases.
>Try the following as an alternative to fflush(stdin).
That seems a bit wasteful of storage since you have no intention of using the data. Especially since most floating data is considerably less than 80 characters. And you don't have to worry about making the second argument to fgets one less than the buffer, it already reads N - 1 characters and places a nul in the last spot.
-Prelude
My best code is written with the delete key.
I forgot to read one of the posts, so I didn't know that he/she was working with floats.Especially since most floating data is considerably less than 80 characters
Well, I'm a very generous programmer.That seems a bit wasteful of storage since you have no intention of using the data
>so I didn't know that he/she was working with floats
I didn't mean floating-point values, I meant extra characters floating around in the input stream.
>Well, I'm a very generous programmer.
I can't argue with that I suppose. But you may want to update your function to handle a case where the extra input is actually greater than 80 characters.
-Prelude
My best code is written with the delete key.