新手C學生在這裏。isdigit()對於整數10及以上返回true
有人能解釋爲什麼isdigit()返回值爲10+的值爲true嗎? 我正在做一個關於猜謎遊戲的非常基本的任務,並且必須使用isdigit()來通知用戶他是否輸入了數字1-10。 該程序似乎運行良好,否則,我只想知道isdigit()返回值爲10 +的值爲true的原因。
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <time.h>
int main()
{
int iRandomNum = 0;
char cResponse = '0';
srand(time(NULL));
iRandomNum = (rand() % 10) + 1;
printf("\nGuess a number between 1 and 10: ");
scanf("%c", &cResponse);
if (!isdigit(cResponse) || cResponse<'0'+1)
printf("\nYou did not enter a number 1-10");
else if ((cResponse - '0') == iRandomNum)
printf("\nCorrect!");
else
{
printf("\nSorry, you guessed wrong\n");
printf("The correct guess was %d\n", iRandomNum);
}
return 0;
}
只評估第一個字母(數字)(輸入)。 – BLUEPIXY
'10'不是一個數字! 'cResponse <'0'+1' ->'cResponse <'1''。但是,這並不意味着你要麼。 – Olaf