Friday, May 21, 2010

Input without pressing enter in C (in replacement with scanf)?

my code looks like this:





a= getch();





if (a==1)


printf ("1");


if (a==2)


printf ("2");





printf ("%d", a);





but when i do this, it doesn't return the value that i entered in the keyboard. what are some alternatives?

Input without pressing enter in C (in replacement with scanf)?
You are using %d which implies a is numeric.


You haven't defined a.
Reply:a= getch();





if (a=='1')


printf ("1");


if (a=='2')


printf ("2");





printf ("%c", a);
Reply:You are getting a character from the keyboard, not an integer. You need to check an ASCII table to find out what the actual values you are entering are.





The numeral "1" is the ASCII character 49 and the numeral "2" is the ASCII character 50.





if (a == 49)


printf("1");


No comments:

Post a Comment