How to clear stdin?

J

Jim Showalter

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?
 
C

Chris Dollin

Jim said:
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?
 
G

Gordon Burditt

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".
 
D

Default User

Richard said:
Ratan said:


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
 
J

Jim Showalter

<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.
 
K

Keith Thompson

Jim Showalter said:
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.
 
M

Mark McIntyre

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,778
Messages
2,569,605
Members
45,238
Latest member
Top CryptoPodcasts

Latest Threads

Top