That will be because stdin is buffered. So you are probably entering the string of a y
followed by a
(newline character).
So the first iteration takes the y
, but the next iteration doesn't need any input from you because the
is next in the stdin buffer. But you can easily get around this by getting scanf to consume the trailing whitespace.
scanf("%c ",&choice);
NOTE: the space after the c in "%c "
But, your program can get stuck in an infinite loop if the input ends with a y
. So you should also check the result of the scanf
. e.g.
if( scanf("%c ",&choice) <= 0 )
choice = 'n';
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…