473,836 Members | 2,332 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 40275
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.comwrit es:
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_Keit h) 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.comwrot e:
>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
24872
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 information) or perhaps the PHP equivalent of how a Java app would read from standard in:
2
11434
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 buffer while Elaborate(data) is running. I tried to clear the buffer using sys.stdin.flush() but it seem not work.
4
15076
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 application, if I call say scanf() to get some input, occasionally the newline is still left hanging around in the buffer. If there's any junk left over in stdin after getting input I'd like to clear it out. And then when going to read input again later,...
3
2687
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 turning them into float, short, double, etc. files, writing them to an array in sections (so that the machine is working w/ smaller arrays), changing or evaluating the pixel values, and them writing them out. Doing this before, I could only find...
11
14650
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 /* LIST13-6.C CLEARING STDIN OF EXTRA CHARACTERS */ /* USING FFLUSH() FUNCTION */ #include<stdio.h> int main(void)
6
2236
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 library input prompts will not happen if stdin is supplied by pipe or redirection. So, I would like to include a statement in the pluggin
0
2001
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 method exactly works. maybe i have to clear the stdin (tried fflush)!? maybe the is another reason that the former data is displayed. fist i thought of the browser cach, but it's the same problem using ctrl+f5. i need to check if the cgi program...
31
4085
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 standard way to do the same without resorting to OS-specific API calls?
0
9810
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
9656
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10821
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10573
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10241
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9358
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 projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6973
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
4001
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3102
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.