Discussion:
problems with scanf and getchar()
(too old to reply)
Mehes Andrew F J
2004-01-18 03:25:33 UTC
Permalink
In hangman, I have getchar() in a loop to read the character guesses from
the user. However, once the user hits enter, the loop goes on to execute
the rest of the code in the loop, the executres the loop again reading the
user input as a blank and processing this blank before asking for a new
letter. Something similar is also happening in Guessing Game. I was just
wondering if any knew what might be causing this problem. I have been able
to fix it with some if statements, but I am still searching for a reason
for this phenomenon.
Thanks.
Moayad Fahim Ali
2004-01-18 19:54:31 UTC
Permalink
The input from the keyboard is buffered, including the new line character
\n when you press "enter" on your keyboard. So if you press A then
enter, you will be processing two characters (A and \n) and hence the two
loops. If you press A followed by B and C and enter, you'll process the
loop four times.

You can fix this by checking if the current character is an \n and
accordingly process the character or ignore it.
Post by Mehes Andrew F J
In hangman, I have getchar() in a loop to read the character guesses from
the user. However, once the user hits enter, the loop goes on to execute
the rest of the code in the loop, the executres the loop again reading the
user input as a blank and processing this blank before asking for a new
letter. Something similar is also happening in Guessing Game. I was just
wondering if any knew what might be causing this problem. I have been able
to fix it with some if statements, but I am still searching for a reason
for this phenomenon.
Thanks.
Alexander Smith
2004-01-18 22:59:43 UTC
Permalink
There is is another solution for reading a character which you will
probably find easier:

char c;
/* do some stuff; then... */
scanf(" %c", &c);

The space before the %c causes scanf to skip over all whitespace
characters (space, tab, and newline) and get the next non-whitespace
character.

Alexander
Post by Moayad Fahim Ali
The input from the keyboard is buffered, including the new line character
\n when you press "enter" on your keyboard. So if you press A then
enter, you will be processing two characters (A and \n) and hence the two
loops. If you press A followed by B and C and enter, you'll process the
loop four times.
You can fix this by checking if the current character is an \n and
accordingly process the character or ignore it.
Post by Mehes Andrew F J
In hangman, I have getchar() in a loop to read the character guesses from
the user. However, once the user hits enter, the loop goes on to execute
the rest of the code in the loop, the executres the loop again reading the
user input as a blank and processing this blank before asking for a new
letter. Something similar is also happening in Guessing Game. I was just
wondering if any knew what might be causing this problem. I have been able
to fix it with some if statements, but I am still searching for a reason
for this phenomenon.
Thanks.
Diego Huang
2004-01-19 04:34:46 UTC
Permalink
You can also fix that by using putting a newline for scanf to read.

scanf("\n%c", &c);

This way, scanf will ignore the buffered newline.

Diego
Date: Sun, 18 Jan 2004 19:54:31 GMT
Newsgroups: ut.ecf.ece242
Subject: Re: problems with scanf and getchar()
The input from the keyboard is buffered, including the new line character
\n when you press "enter" on your keyboard. So if you press A then
enter, you will be processing two characters (A and \n) and hence the two
loops. If you press A followed by B and C and enter, you'll process the
loop four times.
You can fix this by checking if the current character is an \n and
accordingly process the character or ignore it.
Post by Mehes Andrew F J
In hangman, I have getchar() in a loop to read the character guesses from
the user. However, once the user hits enter, the loop goes on to execute
the rest of the code in the loop, the executres the loop again reading the
user input as a blank and processing this blank before asking for a new
letter. Something similar is also happening in Guessing Game. I was just
wondering if any knew what might be causing this problem. I have been able
to fix it with some if statements, but I am still searching for a reason
for this phenomenon.
Thanks.
Alexander Smith
2004-01-19 17:28:28 UTC
Permalink
I just wanted to point out one detail about how scanf() works. If you
called scanf like this: scanf("q%c", &c), then scanf will skip over the
letter 'q' and then read the next character from stdin. If there isn't a
'q' first, the call to scanf will fail.

However, if you call it like this: scanf("\n%c", &c), then scanf will skip
over *all whitespace characters*. There may be one, several, or zero such
characters. It will then scan the next non-whitespace character into
variable 'c'. The following lines all have exactly the same effect:
scanf(" %c" &c);
scanf("\n%c", &c);
scanf(" \t \n %c", &c);

Alexander

P.S. Whitespace is normally considered to be spaces, tabs, and newlines.
You can use the function isspace() to see whether or not a character is
whitespace. There are other related functions like isalpha(), isdigit(),
isupper(). Run "man isspace" for more info.
Post by Diego Huang
You can also fix that by using putting a newline for scanf to read.
scanf("\n%c", &c);
This way, scanf will ignore the buffered newline.
Diego
Date: Sun, 18 Jan 2004 19:54:31 GMT
Newsgroups: ut.ecf.ece242
Subject: Re: problems with scanf and getchar()
The input from the keyboard is buffered, including the new line character
\n when you press "enter" on your keyboard. So if you press A then
enter, you will be processing two characters (A and \n) and hence the two
loops. If you press A followed by B and C and enter, you'll process the
loop four times.
You can fix this by checking if the current character is an \n and
accordingly process the character or ignore it.
Post by Mehes Andrew F J
In hangman, I have getchar() in a loop to read the character guesses from
the user. However, once the user hits enter, the loop goes on to execute
the rest of the code in the loop, the executres the loop again reading the
user input as a blank and processing this blank before asking for a new
letter. Something similar is also happening in Guessing Game. I was just
wondering if any knew what might be causing this problem. I have been able
to fix it with some if statements, but I am still searching for a reason
for this phenomenon.
Thanks.
Greg Sinclair
2004-01-21 21:56:41 UTC
Permalink
The easiest (i think) way to fix this is to put "fflush ('stdin');" right
before using scanf.

this clears the input buffer.

-Greg
Post by Mehes Andrew F J
In hangman, I have getchar() in a loop to read the character guesses from
the user. However, once the user hits enter, the loop goes on to execute
the rest of the code in the loop, the executres the loop again reading the
user input as a blank and processing this blank before asking for a new
letter. Something similar is also happening in Guessing Game. I was just
wondering if any knew what might be causing this problem. I have been able
to fix it with some if statements, but I am still searching for a reason
for this phenomenon.
Thanks.
Loading...