473,322 Members | 1,736 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,322 software developers and data experts.

How does scanf() read input?

Expand|Select|Wrap|Line Numbers
  1. scanf("%d",&choice);
  2. switch(choice)
  3. {
  4.    case 4:
  5.       printf("Are you sure:");
  6.       scanf("%c",&ch);
  7. }
here ch doesnt take any input character ..i think so it takes the code of the enter key hit for the variable choice. i have come across this difficulty many times when till i dont do fflush() the above problem arises?? y??i do remember having this problem with other input functions also??
Oct 7 '07 #1
7 9417
mattmao
121 100+
scanf("%d",&choice);
switch(choice)
{
case 4:
printf("Are you sure:");
scanf("%c",&ch);
}

here ch doesnt take any input character ..i think so it takes the code of the enter key hit for the variable choice. i have come across this difficulty many times when till i dont do fflush() the above problem arises?? y??i do remember having this problem with other input functions also??
Your code seems fine but you can always do this to clear the keyboard buffer:

while (getchar() != '\n')
;

See if this helps you...
Oct 8 '07 #2
JosAH
11,448 Expert 8TB
scanf("%d",&choice);
The stdin (keyboard) input stream is usually line buffered. When the scanf()
function has to read and convert a decimal number it'll stop reading when a
non-digit is read. You have typed something like "42<enter>"; scanf() has
read 42 for you and leaves the <enter> (a newline character) in the input buffer.

The next time you want to read a character from the keyboard input stream there's
still that <enter> character present and still unread.

kind regards,

Jos
Oct 8 '07 #3
mattmao
121 100+
The stdin (keyboard) input stream is usually line buffered. When the scanf()
function has to read and convert a decimal number it'll stop reading when a
non-digit is read. You have typed something like "42<enter>"; scanf() has
read 42 for you and leaves the <enter> (a newline character) in the input buffer.

The next time you want to read a character from the keyboard input stream there's
still that <enter> character present and still unread.

kind regards,

Jos
Hi Jos.

If that is the case, then this wouldn't work, right?

Expand|Select|Wrap|Line Numbers
  1. while (getchar() !='\n');
So...Use this?
Expand|Select|Wrap|Line Numbers
  1. while (getchar() !='\n');
  2. getchar();
Oct 8 '07 #4
JosAH
11,448 Expert 8TB
Hi Jos.

If that is the case, then this wouldn't work, right?

Expand|Select|Wrap|Line Numbers
  1. while (getchar() !='\n');
So...Use this?
Expand|Select|Wrap|Line Numbers
  1. while (getchar() !='\n');
  2. getchar();
Nonono, getchar() simply reads a single character, so also a '\n' will be read as
in your first example. Scanf() however pushes the first non-digit character back
to the input stream, so if you type "42\n", and scanf using "%d", the \n will stay
in the input stream and must be explicitly read afterwards if you want to read
a non-enter char.

kind regards,

Jos
Oct 8 '07 #5
Nonono, getchar() simply reads a single character, so also a '\n' will be read as
in your first example. Scanf() however pushes the first non-digit character back
to the input stream, so if you type "42\n", and scanf using "%d", the \n will stay
in the input stream and must be explicitly read afterwards if you want to read
a non-enter char.

kind regards,

Jos


thnks for the reply,is there any way i cud do away with this prob wwithout using fflush()??
but if i use

scanf("%d",&var);
scanf("%d",&var);
this gives us the desired output ie input two variables but where does the '\n' go???
Oct 8 '07 #6
thnks for the reply,is there any way i cud do away with this prob wwithout using fflush()??
but if i use

scanf("%d",&var);
scanf("%d",&var);
this gives us the desired output ie input two variables but where does the '\n' go???

scanf("%d",&var);
scanf("%d",&var1);

here i m talking abt the '\n' hit after entering the first variable.where will it go???
here ii m also assuming dat the '\n' hit after enteing the value for the second variables remains in the buffer. is my assumption right???
Oct 9 '07 #7
mattmao
121 100+
If you want to take two integer variables from user keyboard entries, use this:

Expand|Select|Wrap|Line Numbers
  1. printf("please inter two integers, separated by a space in between");
  2. scanf("%d %d", &var1, &var2);
  3. while(getchar()!='\n');
I hope this fit for your situation.




BTW, I heard from my tutor that using ffflush(); is dangerous. check with the manual before using it.
Oct 10 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: kliczko | last post by:
I have php4 on system FreeBSD.I did think that php work very well (function phpinfo() work), if I didn't probe above script: <? if(!isset($name)&&!isset($password)) { ?> <h1>Please...
1
by: Yong Wang | last post by:
Hi, All: I need to read a input file as soucre to process data. Ideally if I can read a line from input file as a list (coloumn separate by white space), reorganize field in a line. Does python...
7
by: hugo27 | last post by:
obrhy8 June 18, 2004 Most compilers define EOF as -1. I'm just putting my toes in the water with a student's model named Miracle C. The ..h documentation of this compiler does state that when...
3
by: tvn007 | last post by:
Please help, basicly, I just want to skip "#" when read line from input file. Below is my code: #include <iostream> #include <string> #include <fstream> #include <sstream> using namespace...
11
by: Alex | last post by:
The input contains two strings alphanumeric ASCII characters separated by whitespace. Input is terminated by EOF.
1
by: Kerem Gümrükcü | last post by:
Hi All, i am too tired now to find this out by my own, but why does my while loop "while(readline==Console.ReadLine())" needs twice return hit to read the line, after setting...
6
by: kalar | last post by:
Hello i have a strange question. space is a character so if we have char ch; scanf("%c", &ch); /*and i press space and enter*/ so now we have in ch = ' '. Am i wrong? if NO then why this...
4
by: Waqas Danish | last post by:
Hello, I am using standard socket functions to send and receive data on a client/server model. I am using K-Develop under Ubuntu 10.04 OS. The application works fine locally but when I deploy it...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.