Wrong solution!!!! Do not use fflush(stdin); . Here's why .
And when asking a question, "... these solutions give me an error" is not an appropriate message to give us. There are hundreds of errors -- be specific, both in the exact error message, and the line the error is on.
WaltP
Posting Sage w/ dash of thyme
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
fflush() on any input stream, like stdin, is deprecated, so it may very well not work at all. Use it on output streams only. ("Flush the toilet, not the kitchen faucet.")
To pull the remaining newline char off the input stream, you can use:
getchar();
right after the scanf() line of code.
Since you know that a newline will always be there, the program will not pause here. It just grabs the newline char, and goes on.
Adak
Nearly a Posting Virtuoso
1,479 posts since Jun 2008
Reputation Points: 425
Solved Threads: 185
fflush() on any input stream, like stdin, is deprecated,
As far as I know, flushing an input stream was neverreprecated (or whatever :icon_mrgreen:), it was never legal -- standards-wise. If it was, why would they remove it? And when?
WaltP
Posting Sage w/ dash of thyme
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
By error, I should have been more specific, it compiles but when it runs, it doesn't work as intended (skips the next scanf()). getchar() also solves the problem.
Which doesn't invalidate the advice to avoid calling fflush() on input streams. getchar() solves the problem as well, but you might consider throwing it into a line-based loop to catch everything rather than just the next bogus character:
void clear_line(FILE *in)
{
int ch;
do
ch = getc(in);
while (ch != '\n' && ch != EOF);
/*
Clear the status if we caused an end-of-file
state, but propagate an error state
*/
if (ch == EOF && !ferror(in))
clearerr(in);
}
One thing to keep in mind is that fflush(stdin) (when it works) doesn't block, but getchar() does. So if the stream is empty and you do something like clear_line(), it will wait for input rather than act as a no-op. There's no portable way to fully simulate the behavior of fflush(stdin), but you can sidestep the need for it by using line input (fgets is the usual recommendation) followed by in-memory parsing (such as with sscanf()) rather than forcing the input function to parse your data too.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401