- 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??
7 9417
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...
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
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? - while (getchar() !='\n');
So...Use this? - while (getchar() !='\n');
-
getchar();
Hi Jos.
If that is the case, then this wouldn't work, right? - while (getchar() !='\n');
So...Use this? - while (getchar() !='\n');
-
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
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???
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???
If you want to take two integer variables from user keyboard entries, use this: - printf("please inter two integers, separated by a space in between");
-
scanf("%d %d", &var1, &var2);
-
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
by: Alex |
last post by:
The input contains two strings alphanumeric ASCII characters separated
by whitespace. Input is terminated by EOF.
|
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...
|
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...
|
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...
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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)...
|
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...
|
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....
|
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...
|
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...
| |