我對C非常陌生,我想弄清楚爲什麼我的代碼返回的值不正確。爲什麼我的代碼返回一個不正確的值?
int main()
{
printf("Welcome to my number generator! \n");
printf("What is the first number in the range? \n");
int rng1 = scanf("%d", &rng1);
printf("What is the second number in the range? \n");
int rng2 = scanf("%d", &rng2);
printf("What would increment would you like to go up in? \n");
int inc = scanf("%d", &inc);
do
{
printf("%d\n"rng1);
rng1 += inc;
}
while(rng1 != rng2);
}
return 0;
}
從這個代碼,我希望第一範圍和第二範圍在一定數量的上升之間的號碼清單,而是我得到1.我做錯了的值? P.S.我試圖'調試'它,發現當我用:
if(isalpha(rng1));
printf("I am a String...")
if(isdigit(rng1))
printf("I am a Digit")
它返回,「我是一個字符串...」。
謝謝!
這些測試*無用*(對不起,雖然不錯)嘗試查找scanf的描述並閱讀它的返回值。 – usr2564301
僅供參考:'if(isalpha(rng1));'在if語句之後不應該有分號 - 'printf'將始終運行,因爲如果條件與分號一致則「結束」。 – ipavl
scanf返回成功初始化變量的數量,因此你的每一個返回1(如果你認爲它是一個有效的輸入數字) 你應該這樣做:'int rng1; scanf(「%d」,&rng1);' –