473,405 Members | 2,338 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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 40236
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Phil Powell | last post by:
In TCL it would be written this way: I guess what I need is the PHP equivalent of TCL's gets command (see http://www.astro.princeton.edu/~rhl/Tcl-Tk_docs/tcl8.0a1/gets.n.html for more...
2
by: Sam | last post by:
Good day! I have a problem with Python and the input buffer. My application must read a barcode from PS2 and then process it. The problem is: i have to discard all characters inserted in...
4
by: Johnathan Doe | last post by:
I'm trying to find a way to reliably peek at stdin, and if anything's waiting, flush stdin so that it clears the stream ready to wait for a character. The problem I have is that in an...
3
by: Josh Wilson | last post by:
OK, somewhat new to C, have searched the group and haven't found an answer that has fixed my problem, so I am just going to try posting it. I am taking really large image files(128x128x35) and...
11
by: Darklight | last post by:
is this correct or incorrect i just read the post below before posting this: In fflush(stdin), what happens to flushed data? this program is taken from sams teach yourself c in 21 days /*...
6
by: ccdrbrg | last post by:
What is the best way to protect stdin within a library? I am writing a terminal based program that provides plugin capability using the dlopen() API. Sequencing program commands (typed) and...
0
by: Oliver Bleckmann | last post by:
hey guys, i have a little problem. my cgi routines are working so far, but i need to redirect to the cgi-programm and what the "posted" data to be cleared if redirected. i don't know how the post...
31
by: Nikos Chantziaras | last post by:
Hello. Is there a way to check if the current process has an stdin handle? In the win32 API, one can do: _eof(_fileno(stdin)) Crucial here is that the above doesn't block. Is there a...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.