ここでコードのエラーを見つけるのを手伝ってくれる人はいますか? 私はプログラミングがまったく初めてで、isdigit()
.
#include <stdio.h>
#include <ctype.h>
main()
{
int iRandom = 0;
int iGuess = 0;
srand(time(NULL));
iRandom = rand()%10 + 1;
printf("\nPlease guess a number between 1 and 10: ");
scanf("%d", &iGuess);
if (isdigit(iGuess)){
if(iGuess == iRandom){
printf("\nYou guessed the correct number!\n");
}
else{
printf("\nThat wasn't the correct number!\n");
printf("\nThe correct number was %d\n", iRandom);
}
}
else{
printf("\nYou did not guess a number.\n");
}
}
問題は、数字を入力するかどうかに関係なく、プログラムが「数字を推測しませんでした」というメッセージを返すことです。gcc コンパイラを実行しても、目立ったエラーは表示されません。ネストされたif
ステートメントが台無しになっている場合isdigit(iGuess)
、trueと評価された場合でもそのelse
部分が実行される理由を誰かが説明できますか?