473,404 Members | 2,137 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.

scanf/cin problem

Hi,
I have written simple code,which will accepts integer input.if i
give character as input,it will not wait for user input next time.what
is the problem in scanf or my code.

Sample Code:

#include <stdio.h>

int main(){
int ch;
while(1){
/* It accepts integer input,if i give character as input,it
will not wait for user input next time */
scanf("%d",&ch)
if(ch == 0){
break;
}
printf("%d",ch);
}
return 0;
}

I have tested this code with Solaris 5.8 with gcc compiler.

Please some one help me to find out problem in code/scanf.

Thanks
Sasikumar
Nov 13 '05 #1
2 3696
SASIKUMAR JP wrote:
Hi,
I have written simple code,which will accepts integer input.if i
give character as input,it will not wait for user input next time.what
is the problem in scanf or my code.

Sample Code:

#include <stdio.h>

int main(){
int ch;
while(1){
/* It accepts integer input,if i give character as input,it
will not wait for user input next time */
scanf("%d",&ch)
if(ch == 0){
break;
}
printf("%d",ch);
}
return 0;
}

I have tested this code with Solaris 5.8 with gcc compiler.

Please some one help me to find out problem in code/scanf.

Thanks
Sasikumar

scanf("%c",&ch)

Nov 13 '05 #2
"SASIKUMAR JP" <jp******@yahoo.co.in> wrote in message
news:c1*************************@posting.google.co m...

I have written simple code,which will accepts integer input.if i
give character as input,it will not wait for user input next time.what
is the problem in scanf or my code.

Sample Code:

#include <stdio.h>

int main(){
int ch;
while(1){
/* It accepts integer input,if i give character as input,it
will not wait for user input next time */
scanf("%d",&ch)
if(ch == 0){
break;
}
printf("%d",ch);
}
return 0;
}


Check the FAQ:

http://www.eskimo.com/~scs/C-faq/q12.19.html

See also:

http://www.comeaucomputing.com/techtalk/#flushinput

Regards,

Russell Hanneken
rh*******@pobox.com
Nov 13 '05 #3

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

Similar topics

4
by: Intaek LIM | last post by:
Hello. I always used cin in C++ for standard input. But, with C, I must use scanf. Integers, chars are all fine with scanf. The problem is... I cannot read floating point value like 'double'...
39
by: Teh Charleh | last post by:
OK I have 2 similar programmes, why does the first one work and the second does not? Basically the problem is that the program seems to ignore the gets call if it comes after a scanf call. Please...
12
by: B Thomas | last post by:
Hi, I was reading O'Reilly's "Practical C programming" book and it warns against the use of scanf, suggesting to avoid using it completely . Instead it recomends to use using fgets and sscanf....
5
by: Eduardo Olivarez | last post by:
The following code does not work correctly on my machine. Either one of the scanf()'s alone work perfectly. However, when they are combined, the second scanf() call just reads what the first one...
33
by: Lalatendu Das | last post by:
Dear friends, I am getting a problem in the code while interacting with a nested Do-while loop It is skipping a scanf () function which it should not. I have written the whole code below. Please...
6
by: obdict | last post by:
Hello, I used scanf() in a while loop, which ensures that user input is valid (must be an integer no greater than 21 or less than 3). If user enters a number out of the range, or enters...
14
by: iwinux | last post by:
Hi. Before I use scanf(), I must malloc the memory for it, like this: //Start char * buffer; buffer = malloc(20); scanf("%s", &buffer); //End
14
by: main() | last post by:
I know this is the problem that most newbies get into. #include<stdio.h> int main(void) { char a; scanf("%c",&a); /*1st scanf */ printf("%c\n",a); scanf("%c",&a); /*2nd scanf*/...
68
by: stasgold | last post by:
Hello. I maybe reinvent the weel ... I'm trying to read positive integer number with the help of scanf, if the input value is not positive number but negaive one zero or char , i have to reread...
13
by: FerrisUML | last post by:
Hello everyone! I new to C and am having the following problem. In the below program, the last scanf is being ignored and the program exits. Can anyone see anything that im doing wrong? Thanks...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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.