scanf ("%c", val2); ---> scanf ("\n%c", val2); on line 25
add \n before %c
because you press enter after entering previous number. you are also giving a new line char "\n" hence the scanf(); is taking "\n" as input.
scanf ("%c", val2); ---> scanf ("\n%c", val2); on line 25
add \n before %c
because you press enter after entering previous number. you are also giving a new line char "\n" hence the scanf(); is taking "\n" as input.
Last edited by sagar474; 11-08-2011 at 04:39 PM.
scanf ("%c", val2); ---> scanf ("\n%c", val2); on line 25
add \n before %c
because you press enter after entering previous number. you are also giving a new line char "\n" hence the scanf(); is taking "\n" as input.
line 15 of your code is wrong :
printf ("read values: %d %c %d", &val1, &val2, &val3);
the address operator (&) cannot be used with an output function such as printf
change it to :
printf ("read values: %d %c %d", val1, val2, val3);
this will print out the correct value
sagar
This worked when I used your suggestion. But to understand this better, why is the scanf() taking the \n as input even before displaying the text - Enter value 2:
Also in the statement scanf (val3); it seems to work without the \n. Is this because now an int is being read rather than a char?
Thanks
When you enter your first digit, 2, you follow it with a newline. The stdin buffer then has '\n'2 waiting.
%d tells scanf() to skip whitespace, read digits from buffer and remove from buffer until non-digit is encountered. So waiting in the buffer is that '\n' which is a valid character for scanf("%c"...) to read.
Specifying "\n%c"... works but I don't recommend using specific text input. What if a user enters 2 space tab enter ?
Just put a leading space after the open quote and before the %c and All leading whitespace will be ignored.