A problem with reading in a character.

G

Gus Tabares

Hello,

I'm having trouble reading in a character. Here is a snippet of code:

int num;
char character;

printf("Enter a number: ");
scanf("%d", &num);
printf("You entered %d.\n", num);

printf("Enter a character: ");
scanf("%c", &character);
printf("You entered %c.\n", character);

When I run this, I get:

Enter a number: 3
You entered 3.
Enter a character: You entered
..

I'm not sure why I'm unable to enter in a character before the last
printf is executed. I'm also tried this with getchar(), with the same
result.

Any help is appreciated.
 
R

Robert Stankowic

Gus Tabares said:
Hello,

I'm having trouble reading in a character. Here is a snippet of code:

int num;
char character;

printf("Enter a number: ");
scanf("%d", &num);
printf("You entered %d.\n", num);

printf("Enter a character: ");
scanf("%c", &character);
printf("You entered %c.\n", character);

When I run this, I get:

Enter a number: 3
You entered 3.
Enter a character: You entered
.
^^^^

Hint:
You have no trouble reading a character, you just read the wrong character
:)
Why is the last dot a line below the prompt?
Can it be, that the printed character is '\n' ?
What exactly was your first input, and where did your first scanf() stop
consuming input?
HTH
Robert
 
K

Keith Thompson

Hello,

I'm having trouble reading in a character. Here is a snippet of code:

int num;
char character;

printf("Enter a number: ");
scanf("%d", &num);
printf("You entered %d.\n", num);

printf("Enter a character: ");
scanf("%c", &character);
printf("You entered %c.\n", character);

When I run this, I get:

Enter a number: 3
You entered 3.
Enter a character: You entered
.

See the C FAQ at <http://www.eskimo.com/~scs/C-faq/top.html>,
particularly question 12.18. I recommend browsing around the rest of
the FAQ while you're there.

(BTW, 12.18 refers to the gets() function without condemning it, but
see also 12.23.)
 
A

Anupam

[email protected] (Gus Tabares) wrote in message news: said:
printf("Enter a number: ");
scanf("%d", &num);
printf("You entered %d.\n", num);

printf("Enter a character: ");
scanf("%c", &character);
printf("You entered %c.\n", character);

When I run this, I get:

Enter a number: 3
Ok what you are really doing here is entering 2 characters, not one.
These are '3' and '\n'. Whenever scanf gets a '\n' it will stop
scanning its input. However it leaves '\n' inside the input queue.
So when it started reading from the input queue it read 3 ... then
said .. ok this is valid... lets see what the next character is ...
its '\n'... That means that I must stop scanning. The '3' which it
read was duly converted to an integer and stored in num.
You entered 3.

Now it prints out that num.

Next line is the crucial one. Remember that it removed '3' from the
input but '\n' still remains.

So when the second scanf looks for an input it finds '\n' and stops
immediatly and the '\n' is read into character.

That is why when you print out the next line, there is a newline
between the "You entered" and the "." .
 
S

sahukar praveen

Anupam said:
(e-mail address removed) (Gus Tabares) wrote in message
[snip]
printf("Enter a number: ");
scanf("%d", &num);
printf("You entered %d.\n", num);

printf("Enter a character: ");
scanf("%c", &character);
printf("You entered %c.\n", character);

When I run this, I get:

Enter a number: 3
Ok what you are really doing here is entering 2 characters, not one.
These are '3' and '\n'. Whenever scanf gets a '\n' it will stop
scanning its input. However it leaves '\n' inside the input queue.
So when it started reading from the input queue it read 3 ... then
said .. ok this is valid... lets see what the next character is ...
its '\n'... That means that I must stop scanning. The '3' which it
read was duly converted to an integer and stored in num.
You entered 3.

Now it prints out that num.

Next line is the crucial one. Remember that it removed '3' from the
input but '\n' still remains.

So when the second scanf looks for an input it finds '\n' and stops
immediatly and the '\n' is read into character.

That is why when you print out the next line, there is a newline
between the "You entered" and the "." .
Enter a character: You entered
.

I am aware that there are many solutions to this (one I can think of is
including a getch() in between the 2 scanfs). However, I am not satisfied
with this method and the extra getch() expects some comments to make the
code more readable.

My question is:

Is there any library function that can flush the standard input at a user
discretion. I read somewhere that fflush(stdin) is an undefined behavior. am
I correct? what is the best solution for this?

Thanks and Regards,
Praveen Kumar
 
A

Anupam

[snip]
I am aware that there are many solutions to this (one I can think of is
including a getch() in between the 2 scanfs). However, I am not satisfied
with this method and the extra getch() expects some comments to make the
code more readable.

The best way would be to write your own function which keeps reading
characters in a loop using _getchar()_ until you get an EOF. However
be sure to check that it was indeed the end of the stream using feof()
.... it might have been a read error. You would be ill-advised to use
getch(). It is not standardised.
My question is:

Is there any library function that can flush the standard input at a user
discretion. I read somewhere that fflush(stdin) is an undefined behavior. am
I correct? what is the best solution for this?

Correct, there is no way as per the standard ( as per my knowledge
of the standard, which may not be perfect ) to remove the characters
waiting at an input queue without reading them and without invoking
UB. The only real solution is the one you mention which is to eat up
the intervening characters by reading them without using them . fflush
is not defined on input streams.
 
C

CBFalconer

sahukar said:
.... snip ...

I am aware that there are many solutions to this (one I can think
of is including a getch() in between the 2 scanfs). However, I am
not satisfied with this method and the extra getch() expects some
comments to make the code more readable.

My question is:

Is there any library function that can flush the standard input
at a user discretion. I read somewhere that fflush(stdin) is an
undefined behavior. am I correct? what is the best solution for
this?

scanf is not generally suitable for interactive input. You have
two basic choices. Input a complete line and parse it with
sscanf, or use getc and ungetc and your own conversion routines
from stream input. The first has the problem of reading the
complete line, of unknown length, and fgets is not completely
suitable. The second can be highly efficient, but requires you
write all the conversion routines correctly. My ggets can help
with the first, as I pointed out elsewhere.
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top