How to clear stdin

G

galapogos

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

Richard Heathfield

galapogos said:
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.
*/
 
M

Mohan

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
 
R

Richard Heathfield

Richard Heathfield said:
/* 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...
 
M

Mohan

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
 
G

galapogos

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

Flash Gordon

galapogos wrote, On 15/05/07 17:53:
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.
 
B

Bart van Ingen Schenau

Mohan said:
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
....
} while (expr);

hope this helps.
Mohan

Bart v Ingen Schenau
 
F

Francine.Neary

galapogos said:


/* 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;
}

Isn't this line redundant? A simple
while((ch = getc(fp)) != EOF && ch != '\n');
could be clearer.
 
R

Richard Heathfield

(e-mail address removed) said:
On May 15, 9:57 am, Richard Heathfield <[email protected]> wrote:

Isn't this line redundant?

I don't think so. It has documentary value.
A simple
while((ch = getc(fp)) != EOF && ch != '\n');
could be clearer.

For certain values of "clearer", perhaps. I consider a semicolon just
there to be most ill-advised. If you're going to do it that way, at
least place the semicolon on the next line and indent it.
 
G

galapogos

This will go in an endless loop if the user triggers an End-Of-File



Bart v Ingen Schenau

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

galapogos

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? c is actually of type char right now
since I figure I'm just getting a single ASCII char.
 
R

Richard Heathfield

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

Mohan

Mohan wrote:


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.

Oops! you are right, forgot to handle EOF.

Mohan
 
R

Richard Heathfield

CBFalconer said:
Or insert the 'continue' just before the final semi-colon.

Right - and then, as a final tweak, follow the common (although not
universal) convention of always using a compound statement for the body
of a loop, and you have my original code again.
 
T

Tor Rustad

galapogos said:
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?

For example

scanf("%*[^\n]");
getchar();

or

while ((c=getchar()) != '\n' && c != EOF)
;
 
J

Joseph Alten

galapogos said:
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
 

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

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top