473,404 Members | 2,213 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

A problem with reading in a character.

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.
Nov 13 '05 #1
6 6189

"Gus Tabares" <gu********@verizon.net> schrieb im Newsbeitrag
news:c4**************************@posting.google.c om...
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
Nov 13 '05 #2
gu********@verizon.net (Gus Tabares) writes:
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.)

--
Keith Thompson (The_Other_Keith) ks*@cts.com <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
Nov 13 '05 #3
gu********@verizon.net (Gus Tabares) wrote in message news:<c4**************************@posting.google. com>...
[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
.

Nov 13 '05 #4

"Anupam" <an**************@persistent.co.in> wrote in message
news:aa*************************@posting.google.co m...
gu********@verizon.net (Gus Tabares) wrote in message news:<c4**************************@posting.google. com>... [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
Nov 13 '05 #5
"sahukar praveen" <sa************@yahoo.co.in> wrote in message news:<3f******@usenet01.boi.hp.com>...

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

Thanks and Regards,
Praveen Kumar

Nov 13 '05 #6
sahukar praveen wrote:
.... 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.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 13 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Matt Chaplain | last post by:
Hi there. I'm writing a program that uses the Telnet protocol over TCP/IP sockets. Of course, that has no bearing here, so I'll rephrase that in Standard C++ :) In essense, I'm trying to...
2
by: Tom | last post by:
OK I'm trying to go to the end of a file and delete the contents upwards until I meet a certain character. How can I do this? (NOTE: I'm using text files)
1
by: Need Helps | last post by:
Hello. I'm writing an application that writes to a file a month, day, year, number of comments, then some strings for the comments. So the format for each record would look like:...
2
by: SteMc | last post by:
today I tackled, for the first time, opening and reading from a text file. Following the example on the MSDN and declared a variable, strline as a string and objstreamreader as a streamreader. ...
3
by: Mike | last post by:
I seem to be having a problem with the following piece of (simple) code: """ #include <iostream> using namespace std; int main() { char ch;
2
by: shblack | last post by:
I need help with a program I am writing for school. The program has to do the following: /* 1. Character string must be a minimium of 15 charactors. 2. If not 15 characters long it must give...
10
by: hsmit.home | last post by:
Hi everyone, I'm having some difficulty with the following piece of code. I have stripped it to it's bare minimum to demonstrate the problem at hand. Compiler: MS Visual C++ 2005 Express...
9
by: Amkcoder | last post by:
I am trying to read a file a character at a time, I want to read in the new line character. I am printing out each character to the screen. For some reason it will not print out the new line. I...
7
by: tshad | last post by:
I have a program in 2005 that is reading a text file removing text and then writing it back out again. It removes lines that start with PRINT. This program has worked fine for months. Now all...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.