scanf/cin problem

S

SASIKUMAR JP

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
 
M

Materialised

SASIKUMAR said:
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)
 
R

Russell Hanneken

SASIKUMAR JP said:
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
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top