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 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)
"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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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'...
|
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...
|
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....
|
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...
|
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...
|
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...
|
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
|
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*/...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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...
| |