2013-03-04 129 views
1

這是一個家庭作業問題,但我只是要求調試幫助。我不確定是什麼導致了這些錯誤。在C編程中循環練習

int CalculateResult(int High, int Low) 
{ 
    int Result; 
    int count = 0; 
    int check; 
do 
    { 
    printf("Enter the value to check within the range: "); 
    scanf("%d",&Result); 

    if (Result == 0) 
    { 
    printf("Error! Positive value only!\n"); 
    } 

    else if (Result < -1) 
    { 
    printf("Error! Positive value only!\n"); 
    } 

    else if (Result>= 1); 
    { 
    for (check = Low ; check <= High;check++) 
     { 
     if (check%Result==0) 
      { 
      (count++); 
      } 
     } 
    } 
    while (Result != -1); 
    return (count); 
    } 
} 

我有兩個錯誤:174:1 ---而在此之前}令牌 預計再有就是174:1點---預計在聲明輸入結束

任何人看到什麼是錯的?對不起,它很混亂,我是一個小菜。

+6

這不是一個調試問題。這是一個語法錯誤的問題。 – 2013-03-04 22:29:31

+2

匹配你的'{'和'}'。你會很容易看到錯誤。提示:有一個'do {statements} while(contition)'語句和一個不同的'while(condition){語句}'stamement。 – pmg 2013-03-04 22:31:40

+0

你的代碼片段的哪一行對應於第174行? – SirPentor 2013-03-04 22:32:01

回答

1

如果你正確地縮進代碼,你可以很容易地看到那裏的錯誤是:

int CalculateResult(int High, int Low) 
{ 
    int Result; 
    int count = 0; 
    int check; 
    do 
    { 
    printf("Enter the value to check within the range: "); 
    scanf("%d",&Result); 

    if (Result == 0) 
    { 
     printf("Error! Positive value only!\n"); 
    } 

    else if (Result < -1) 
    { 
     printf("Error! Positive value only!\n"); 
    } 

    else if (Result >= 1); 
    { 
     for (check = Low ; check <= High; check++) 
     { 
      if (check%Result == 0) 
      { 
       (count++); <-------- The() are OK, but not necessary. 
      } 
     } 
    } 
    } <--------------------------- Moved this brace up from below. 
    while (Result != -1); 

    return (count); 
} 
+0

我不會說在if-block中沒有額外的縮進是「正確的」,但是..一個品味的問題 – 2013-03-04 22:41:14

+0

我認爲他期待在do-while循環結束後返回(計數)。根據你的答案,它不會工作。 – 2013-03-04 22:45:24

+0

@Nicklas這是Allman風格。它的優點是可以讓你排列你的開啓和關閉括號以進行比較。 OP似乎使用Whitesmiths風格(或多或少)。 – 2013-03-04 22:48:58

1

在指令前添加「}」。你丟失了一個括號:)現在,「while」是最後一個「else if」指令。正確的模式: do {
//Some code here
} while (clause);

+0

這麼多的COMMOLLOL謝謝大家。我根據您的所有說明修復了代碼,我很感激。 – Liquidmetal 2013-03-04 23:09:50

1

變化

 } 
    while (Result != -1); 
    return (count); 
    } 
} 

 } 
    } 
    while (Result != -1); 
    return (count); 
} 
0

只有要說兩件事:
1.檢查是否有所有需要的括號
2.檢查是否缺少「;」或者如果有「;」它不應該
這是一個家庭作業的問題,所以我不會給你答案直接
好了:),再提一些建議:
1.總是縮進代碼
2. ifelse if不一個塊做任何事情可能是沒用的ifelse if;)