2014-12-27 95 views
0

我對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") 

它返回,「我是一個字符串...」。

謝謝!

+0

這些測試*無用*(對不起,雖然不錯)嘗試查找scanf的描述並閱讀它的返回值。 – usr2564301

+5

僅供參考:'if(isalpha(rng1));'在if語句之後不應該有分號 - 'printf'將始終運行,因爲如果條件與分號一致則「結束」。 – ipavl

+3

scanf返回成功初始化變量的數量,因此你的每一個返回1(如果你認爲它是一個有效的輸入數字) 你應該這樣做:'int rng1; scanf(「%d」,&rng1);' –

回答

2

你是通過調用scanf並且scanf已經將輸入值分配給變量rng1,rng2inc,並且scanf返回成功填充的參數列表的項目數量。因此,將scanf的返回值分配給這些變量是不正確的。只需使用輸入數量的返回值即可。您應該讀取值1,因爲您只想讀取每個scanf的一個值。此外,您還可以檢查輸入值以檢測輸入值是否有效。

除此之外,我想對您的代碼進行一些修改。尤其是您的do{...}while();循環可能由於運算符!=而無限期地運行。請參閱下面的代碼中的評論。

int main() 
{ 
    /* Declare the variables and do not assign the return value of scanf */ 
    int rng1, rng2, inc; 
    printf("Welcome to my number generator! \n"); 
    printf("What is the first number in the range? \n"); 
    /* repeat this check condition for each scanf, exit(EXIT_FAILURE) requires #include <stdlib.h>*/ 

    if (1 != scanf("%d", &rng1)) { 
     exit(EXIT_FAILURE); 
    } 
    printf("What is the second number in the range? \n"); 
    scanf("%d", &rng2); 
    printf("What would increment would you like to go up in? \n"); 
    scanf("%d", &inc); 

    do 
    { 
     printf("%d\n",rng1); 
     rng1 += inc; 
    } 
    /* Use <= instead of != and think about the case for rng1 is 1 rng2 is 5 and inc is 3, can you detect the end of the loop by adding 3 to the starting point 1 ? */ 
    while(rng1 <= rng2); 

    /* Remove the `}` here */ 
    return 0; 
} 
+0

謝謝你現在好像工作得很好。所以問題是我覆蓋了變量的值,而不是讀取scanf的返回值並將其設置爲變量的值? – Dinar

+0

對於奇怪的價值問題,是的。但是,您還應該檢查while(rng1 <= rng2);在您的原始代碼中。 – Deniz

1

當您傳遞變量的地址(使用&運算符)時,允許scanf將掃描值寫入變量。 scanf的返回值是而不是您正在查找的值(這是別的)。

因此,當您將scanf的返回值分配給您的變量時,您將覆蓋它已有的正確值。這應該工作:

int rng1; 
scanf("%d", &rng1); 
+1

但是您應該檢查返回值以確保您讀取了正確的i電信設備製造商。 – clcto

+0

因此,通過使用'='符號,我用不同的結果而不是用戶輸入覆蓋了'int rng1'的值? – Dinar

+0

@Dinar是的。其他人已經描述了'scanf'的實際返回值。您可能會使用該結果,或者您現在可能會選擇忽略它,但無論如何您都不應該覆蓋'scanf'已將用戶輸入的變量。 –

1

scanf()返回的值應始終進行檢查,以確保手術成功。 格式字符串應該(幾乎)總是包含一個前導空格「」 這樣一個遺留的白色空間(如新行)被跳過/消耗 因此,代碼應該更像:

int rng1; 
if(1 != scanf(" %d", &rng1)) 
{ // then, scanf failed 
    perror("scanf for rng1 failed"); 
    exit(EXIT_FAILURE); 
} 

// implied else, scanf for rng1 successful 
0

AS上面兩個註釋解釋了scnaf()有返回值只是爲了檢查成功的操作。然而,你需要閱讀的價值是作爲第二個參數。 我也可以看到代碼中的括號不匹配。

int main(int argc, char **argv) 
{ 
    printf("Welcome to my number generator! \n"); 
    printf("What is the first number in the range? \n"); 
    int rng1; 
    int t = scanf("%d", &rng1);     // First scanned value stored in rng1 variable 
    printf("What is the second number in the range? \n"); 
    int rng2; 
    int t1 = scanf("%d", &rng2);     // Second scanned value stored in rng2 variable 
    printf("What would increment would you like to go up in? \n"); 
    int inc; 
    int t2 = scanf("%d", &inc);     // Third scanned value stored in inc variable 

    do 
    { 
     printf("%d\n",rng1); 
     rng1 += inc; 
    } 
    while(rng1 <= rng2);    // repeat until rng1 reaches value of rng2 

    return 0; 
} 

這裏使用臨時變量t,t1和t2,您可以驗證值的掃描是否成功完成。

相關問題