Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > How to clear stdin?

Reply
Thread Tools

How to clear stdin?

 
 
Jim Showalter
Guest
Posts: n/a
 
      09-08-2006
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?
 
Reply With Quote
 
 
 
 
Chris Dollin
Guest
Posts: n/a
 
      09-08-2006
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.

 
Reply With Quote
 
 
 
 
Ratan
Guest
Posts: n/a
 
      09-08-2006
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?


 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      09-08-2006
[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)
 
Reply With Quote
 
Gordon Burditt
Guest
Posts: n/a
 
      09-08-2006
>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".

 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      09-08-2006
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
 
Reply With Quote
 
Jim Showalter
Guest
Posts: n/a
 
      09-08-2006
<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.


 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      09-08-2006
Jim Showalter <> writes:
> 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) kst- <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.
 
Reply With Quote
 
Mark McIntyre
Guest
Posts: n/a
 
      09-08-2006
On Fri, 08 Sep 2006 10:58:11 GMT, in comp.lang.c , 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?


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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Firefox - Clear History? Paull Firefox 7 08-24-2012 04:49 PM
Response.Clear() doesn't clear David ASP .Net 2 01-31-2008 08:32 PM
Unrecognized element 'add' after <clear></clear> InvalidLastName ASP .Net Web Services 3 03-06-2007 03:07 AM
Pay after confirmation(we clear ur microsoft and cisco exams)100% passing gaurantee vinayji Wireless Networking 0 09-08-2005 08:18 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57