I wanted the help with the getchar() function.
Printable View
I wanted the help with the getchar() function.
There is an FAQ on this on this site.
Code:int c;
c = getchar();
if(c != EOF)
{
/* Do something with char.... */
}
There is one thing to note when mixing getchar() with other input functions. scanf() often leaves extra characters in stdin: for example, if you typed "10\n" and scanf() was looking for a number, it would take the "10" but leave "\n" in the input stream. Then if you used getchar() immediately afterwards to get a character, you'd end up with the newline, which was probably not what you intended.
If you use scanf() and then getchar(), you can eliminate any characters that scanf() may have left behind like so:
[edit]Code:int c;
while((c = getchar()) != '\n' && c != EOF); /* flush the input stream */
c = getchar(); /* the actual character */
Do you still want help? ;) [/edit]Quote:
I wanted the help with the getchar() function.
What are you trying to do?
No, it doesn't in the sense that it will block for input and accept new input just to throw it away.
Only call it when you are aware that the input stream should contain at least one excess char.
but how to solve it when it is unknown wether the stdin buffer is empty or not empty?
my solution is to set buffering to NULL before getchar(), but I don't think that is a nice solution
You should really read the FAQ entry on this subject.
Here. I'll do the work for you :
http://faq.cprogramming.com/cgi-bin/...&id=1043284392
and
http://faq.cprogramming.com/cgi-bin/...&id=1043284385