468,490 Members | 2,603 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,490 developers. It's quick & easy.

How to clear stdin?

I use getchar() to get the user's menu choice. One of the
choices is to enter strings of information, for which I use
fgets(str, 21, stdin). But when it's called, fgets() gets
a null string. The previous use of getchar() evidently
doesn't clear stdin. I've tried using fflush(stdin) before
the call to fgets(), but it makes no difference.

How do I prevent this undesirable behavior?
Sep 8 '06 #1
8 38690
go to below link.
http://www.velocityreviews.com/forum...ush-stdin.html

I think this will solve your problem.
Jim Showalter wrote:
I use getchar() to get the user's menu choice. One of the
choices is to enter strings of information, for which I use
fgets(str, 21, stdin). But when it's called, fgets() gets
a null string. The previous use of getchar() evidently
doesn't clear stdin. I've tried using fflush(stdin) before
the call to fgets(), but it makes no difference.

How do I prevent this undesirable behavior?
Sep 8 '06 #2
Jim Showalter wrote:
I use getchar() to get the user's menu choice. One of the
choices is to enter strings of information, for which I use
fgets(str, 21, stdin). But when it's called, fgets() gets
a null string. The previous use of getchar() evidently
doesn't clear stdin.
Why should it?
I've tried using fflush(stdin) before
the call to fgets(), but it makes no difference.
It does: it makes your program's behaviour undefined.
How do I prevent this undesirable behavior?
read the newline before calling fgets?

--
Chris "seeker" Dollin
A rock is not a fact. A rock is a rock.

Sep 8 '06 #3
[Top-posting fixed]

Ratan said:
Jim Showalter wrote:
<snip>
>I've tried using fflush(stdin) before
the call to fgets(), but it makes no difference.

How do I prevent this undesirable behavior?

go to below link.
http://www.velocityreviews.com/forum...ush-stdin.html
I think this will solve your problem.
Although the URL does not look promising, it appears to be a dump of a
Usenet thread, in which Chris Torek gives an excellent article on the
stdin-clearing problem. The OP need look no further than that URL on this
occasion.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Sep 8 '06 #4
>I use getchar() to get the user's menu choice. One of the
>choices is to enter strings of information, for which I use
fgets(str, 21, stdin). But when it's called, fgets() gets
a null string.
A string with a newline character in it is *NOT* a null string.
A newline is a real character. Believe it. Worship it.
>The previous use of getchar() evidently
doesn't clear stdin.
Nothing is supposed to "clear stdin". If the user took the trouble
to type it, you should pay attention to it, if only to skip over it.
>I've tried using fflush(stdin) before
the call to fgets(), but it makes no difference.
fflush((void) main) will be more predictable and save you time
(it shouldn't compile).
>How do I prevent this undesirable behavior?
Don't even think about "clearing stdin".

Sep 8 '06 #5
Richard Heathfield wrote:
Ratan said:
http://www.velocityreviews.com/forum...din-flush-stdi
n.html
Although the URL does not look promising, it appears to be a dump of
a Usenet thread, in which Chris Torek gives an excellent article on
the stdin-clearing problem. The OP need look no further than that URL
on this occasion.
It's not a dump, it's one of those usenet->web forum setups.


Brian
Sep 8 '06 #6
<posted & mailed>

Thanks for the link to Chris Torek's article, Richard and Ratan.
It solved the problem and was enlightening as well.

Thanks also for the criticisms and admonishments. I don't mind -
I know I have much to learn.
Sep 8 '06 #7
Jim Showalter <ji***********@hotmail.comwrites:
I use getchar() to get the user's menu choice. One of the
choices is to enter strings of information, for which I use
fgets(str, 21, stdin). But when it's called, fgets() gets
a null string. The previous use of getchar() evidently
doesn't clear stdin. I've tried using fflush(stdin) before
the call to fgets(), but it makes no difference.

How do I prevent this undesirable behavior?
I see you've already fixed the problem, but for future reference we
can help you much better if you'll post actual code. If you don't
understand the code yourself, it's highly unlikely that you'll be able
to describe it accurately enough for us to diagnose it.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Sep 8 '06 #8
On Fri, 08 Sep 2006 10:58:11 GMT, in comp.lang.c , Jim Showalter
<ji***********@hotmail.comwrote:
>I use getchar() to get the user's menu choice. One of the
choices is to enter strings of information, for which I use
fgets(str, 21, stdin). But when it's called, fgets() gets
a null string. The previous use of getchar() evidently
doesn't clear stdin. I've tried using fflush(stdin) before
the call to fgets(), but it makes no difference.

How do I prevent this undesirable behavior?
This is a FAQ.
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Sep 8 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Phil Powell | last post: by
2 posts views Thread by Sam | last post: by
4 posts views Thread by Johnathan Doe | last post: by
3 posts views Thread by Josh Wilson | last post: by
11 posts views Thread by Darklight | last post: by
6 posts views Thread by ccdrbrg | last post: by
reply views Thread by Oliver Bleckmann | last post: by
31 posts views Thread by Nikos Chantziaras | last post: by
reply views Thread by NPC403 | last post: by
3 posts views Thread by gieforce | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.