 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
galapogos Guest
|
Posted: Tue May 15, 2007 9:12 am Post subject: How to clear stdin |
|
|
Hi,
I'm trying to prompt a user to press enter to continue by calling
getchar(), but if the user types in some random string, it messes up
my program since I have a do-while loop that calls getchar() later on
for user input. How do I clear the stdin to prevent this?
Thanks. |
|
| Back to top |
|
 |
Mohan Guest
|
Posted: Tue May 15, 2007 9:12 am Post subject: Re: How to clear stdin |
|
|
On May 15, 1:38 pm, galapogos <gois...@gmail.com> wrote:
| Quote: | Hi,
I'm trying to prompt a user to press enter to continue by calling
getchar(), but if the user types in some random string, it messes up
my program since I have a do-while loop that calls getchar() later on
for user input. How do I clear the stdin to prevent this?
|
if I understand correctly, you want to ignore characters entered other
than new line.
if so:
do
{
printf ("prompt> ");
/* wait until new-line is entered, ignore/clear all other
characters */
while ((c = getchar ()) != '\n');
....
} while (expr);
hope this helps.
Mohan |
|
| Back to top |
|
 |
Richard Heathfield Guest
|
Posted: Tue May 15, 2007 9:12 am Post subject: Re: How to clear stdin |
|
|
Richard Heathfield said:
| Quote: | /* clreol - removes all characters from a stream up to and including
* the next newline character. If the end of the stream is
encountered, * this function returns 1. Otherwise it returns 0.
|
insert a comment-closer */ here - which of course demonstrates that I
didn't even compile this, let alone test it...
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www. |
|
| Back to top |
|
 |
Richard Heathfield Guest
|
Posted: Tue May 15, 2007 9:12 am Post subject: Re: How to clear stdin |
|
|
galapogos said:
| Quote: | Hi,
I'm trying to prompt a user to press enter to continue by calling
getchar(), but if the user types in some random string, it messes up
my program since I have a do-while loop that calls getchar() later on
for user input. How do I clear the stdin to prevent this?
|
/* clreol - removes all characters from a stream up to and including
* the next newline character. If the end of the stream is encountered,
* this function returns 1. Otherwise it returns 0.
#include <stdio.h>
int clreol(FILE *fp)
{
int ch;
while((ch = getc(fp)) != EOF && ch != '\n')
{
continue;
}
return ch == EOF;
}
Usage:
puts("Press ENTER to continue...");
if(clreol(stdin))
{
/* EOF encountered! If you need input
* from this stream, you're out of
* luck, and might as well quit.
*/
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www. |
|
| Back to top |
|
 |
galapogos Guest
|
Posted: Wed May 16, 2007 9:11 am Post subject: Re: How to clear stdin |
|
|
On May 16, 1:53 am, Bart van Ingen Schenau <b...@ingen.ddns.info>
wrote:
| Quote: | Mohan wrote:
On May 15, 1:38 pm, galapogos <gois...@gmail.com> wrote:
Hi,
I'm trying to prompt a user to press enter to continue by calling
getchar(), but if the user types in some random string, it messes up
my program since I have a do-while loop that calls getchar() later on
for user input. How do I clear the stdin to prevent this?
if I understand correctly, you want to ignore characters entered other
than new line.
if so:
do
{
printf ("prompt> ");
/* wait until new-line is entered, ignore/clear all other
characters */
while ((c = getchar ()) != '\n');
This will go in an endless loop if the user triggers an End-Of-File
condition instead of pressing <Enter>.
See Richard's answer for a similar solution that does handle EOF.
....
} while (expr);
hope this helps.
Mohan
Bart v Ingen Schenau
--
a.c.l.l.c-c++ FAQ:http://www.comeaucomputing.com/learn/faq
c.l.c FAQ:http://www.eskimo.com/~scs/C-faq/top.html
c.l.c++ FAQ:http://www.parashift.com/c++-faq-lite/
|
Thanks for all the help...
I ended up doing this:
do {
// some stuff that I do here
printf("Press q to quit or Enter to continue\n");
do {
ch = getchar();
} while ( (ch != 'q') && (ch != '\n') );
} while ( (ch != 'q') )l
This works to a certain extent, in that it scans until it finds a 'q'
or enter, so if the user enters "skjdhfjsk\n" then it would loop back,
but if he enters "sdhajkqsld\n" it would quit since there's a q in the
string. Not the best solution but works well enough I think. |
|
| Back to top |
|
 |
galapogos Guest
|
Posted: Wed May 16, 2007 9:11 am Post subject: Re: How to clear stdin |
|
|
On May 16, 1:51 am, Flash Gordon <s...@flash-gordon.me.uk> wrote:
| Quote: | galapogos wrote, On 15/05/07 17:53:
On May 15, 5:12 pm, Mohan <ajm...@gmail.com> wrote:
On May 15, 1:38 pm, galapogos <gois...@gmail.com> wrote:
Hi,
I'm trying to prompt a user to press enter to continue by calling
getchar(), but if the user types in some random string, it messes up
my program since I have a do-while loop that calls getchar() later on
for user input. How do I clear the stdin to prevent this?
if I understand correctly, you want to ignore characters entered other
than new line.
if so:
do
{
printf ("prompt> ");
/* wait until new-line is entered, ignore/clear all other
characters */
while ((c = getchar ()) != '\n');
....
} while (expr);
hope this helps.
Mohan
Thanks. Actually I have 2 options, Enter will continue the loop, while
Q or q will quite the program, so I guess I can add those to the
while() test.
You almost certainly want to make sure c is of type int and check for
EOF as well, otherwise if the user signals end-of-file you will have an
infinite loop.
--
Flash Gordon
|
How would the user signal an EOF? c is actually of type char right now
since I figure I'm just getting a single ASCII char. |
|
| Back to top |
|
 |
Richard Heathfield Guest
|
Posted: Wed May 16, 2007 9:11 am Post subject: Re: How to clear stdin |
|
|
galapogos said:
| Quote: | On May 16, 1:51 am, Flash Gordon <s...@flash-gordon.me.uk> wrote:
galapogos wrote, On 15/05/07 17:53:
You almost certainly want to make sure c is of type int and check for
EOF as well, otherwise if the user signals end-of-file you will have
an infinite loop.
How would the user signal an EOF?
|
If he's directing input from a file, he wouldn't need to - it would
happen anyway when the OS ran out of file.
If he's typing stuff in, typically ^Z (Win32) or ^D (Linux) will do it,
although the exact details depend on your exact setup.
| Quote: | c is actually of type char right now
since I figure I'm just getting a single ASCII char.
|
Here's the prototype:
int getchar(void);
Why do you think getchar, despite the name, returns int? If you're
thinking "because whoever designed the function is stupid", think
again. If you can't imagine why, go find out. Both the search itself
and the result of that search will be instructive.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www. |
|
| Back to top |
|
 |
Joseph Alten Guest
|
Posted: Sat May 19, 2007 8:30 am Post subject: Re: How to clear stdin |
|
|
galapogos wrote:
| Quote: |
Thanks. Actually I have 2 options, Enter will continue the loop, while
Q or q will quite the program, so I guess I can add those to the
while() test.
|
Actually, it's best to avoid the need to clear stdin in the first place.
For example, one could use fgets() (my preferred method), and then just
grab the first character to see if it's a q.
Although there are some circumstances where it's absolutely essential to
clear it, I try to avoid the need to do so whenever possible.
Hope this helps.
--Joe |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|