The basic problem is that scanf()
leaves the newline after the number in the buffer, and then reads it with %c
on the next pass. Actually, this is a good demonstration of why I don't use scanf()
; I use a line reader (fgets()
, for example) and sscanf()
. It is easier to control.
You can probably rescue it by using " %c"
instead of "%c"
for the format string. The blank causes scanf()
to skip white space (including newlines) before reading the character.
But it will be easier in the long run to give up scanf()
and fscanf()
and use fgets()
or equivalent plus sscanf()
. All else apart, error reporting is much easier when you have the whole string to work with, not the driblets left behind by scanf()
after it fails.
You should also, always, check that you get a value converted from scanf()
. Input fails — routinely and horribly. Don't let it wreck your program because you didn't check.