scanf("%i", &input);
scanf
with the format %i
converts its input to an integer. If the user presses 4 2 Enter, that makes the scanf
call equivalent to input = 42
.
If the input can't be converted to an integer, scanf
returns EOF
and doesn't set input
. Since you don't check the return value of scanf
, your program continues with an indeterminate value in input
. (In theory that's undefined behavior, but in practice your program will run with whatever happened to be in the memory location of input
.)
if(!isdigit(input)){
This tests whether the number input
is the numerical code of a digit character. In practice, the correspondence between characters and their numerical codes on your computer is ASCII, so digits occupy the range from 48 to 57 inclusive. So the body of this if
clause only runs if input
is not between 48 and 57.
I have no idea why you'd do that. If you meant to check whether the input is valid, check the return value of scanf
. If you thought you were checking the first character of the input, then 1. no you aren't, you're checking the result of the conversion, you can't access the raw input from the user; and 2. you'd only be checking one character anyway so your check couldn't possibly be correct.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…