Reputation: 1729
I used to use fflush(stdin)
. I read that this is not a good way to get rid of the extra characters and that it is better to use fgets like this:
fgets(buffer,maxsize,stdin);
In cases that I want to dispose of those extra chars...what kind of buffer should I use? Could I redirect in some kind of "buffer of no return"? Or do I have to use a finite size array?
Thanks in advance.
Upvotes: 0
Views: 11003
Reputation: 108978
use consumetoendofline(stdin)
instead :)
int consumetoendofline(FILE *where) {
int ch;
while (((ch = fgetc(where)) != '\n') && (ch != EOF)) /* void */;
return ch;
}
You can (and should) even test the return value to see if the stream has reached its end, or if there is probably more data waiting ...
Upvotes: 2