I wanted the help with the getchar() function.
I wanted the help with the getchar() function.
There is an FAQ on this on this site.
Double Helix STL
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]I wanted the help with the getchar() function.
Last edited by dwks; 05-24-2007 at 03:04 PM.
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
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
Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction