2014-09-12 66 views
0

因此設計問題是:查找用戶在C++中輸入的最大數字的平均值3

輸入3個數字並顯示最大值。繼續輸入3組數字,直到用戶想要退出。查找所有最大數字的平均值。

所以輸入數字能正常工作,但是當我試圖插入SENTINEL值來停止循環時,我需要爲所有3個數字輸入它,並且它不會給我輸入的過去數字的正確平均值。

任何幫助非常感謝您的時間!

#include <iostream> 
using namespace std; 

int main() 
{ 

    const int SENTINEL = -1; 
    int num1; 
    int num2 = 0; 
    int num3 = 0; 
    int largest; 
    int sum; 
    int count; 
    float average; 

    // initialize the count and sum 
    count = 0; 
    sum = 0; 

    while (num1 != SENTINEL) 
    { 

     // Prompt user for the first number or to quit 
     cout << "If you want to quit enter " << SENTINEL << " to stop\n " << endl; 
     cout << "Enter first number "; 
     cin >> num1; 

     // Prompt the user for the second number 
     cout << "please enter second number     "; 
     cin >> num2; 

     // Prompt the user for a third number 
     cout << "please enter the third number    "; 
     cin >> num3; 

     // Compare numbers 1 and 2 for the largest number 
     if (num1 > num2) 
     { 
      largest = num1; 
     } 
     else 
     { 
      largest = num2; 
     } 

     // Compare largest to last number input 
     if (num3 > largest) 
     { 
      largest = num3; 
     } 

     // Display the largest number 
     cout << "largest number is: " << largest << endl; 

     // Increment the count 
     count++; 

    } 



    if (count > 0) 
    { 
     average = sum/count; 
     cout << "Average of the numbers is " << average; 
    } 

    return 0; 
} 
+2

在使用它之前,您不會初始化num1,這會導致[* undefined behavior *](http://en.wikipedia.org/wiki/Undefined_behavior)。你也不會增加總和。 – 2014-09-12 19:11:56

+0

我會改變它初始化num1謝謝我意識到我還沒有增加總和,但代碼是不完整的問題,我正在做它的碎片和測試它,並被卡在這部分.. – RJH 2014-09-12 19:37:01

回答

0

輸入num1後立即有條件地破環。

cin >> num1; 
if (SENTINEL == num1) 
{ 
    break; 
} 
+0

我可以' t使用中斷,除非它的每個教師的switch語句我試過了lol – RJH 2014-09-12 19:33:37

+0

然後使用'continue;'代替:)它跳轉到下一個迭代,然後檢查SENTINEL – Surt 2014-09-12 20:44:23

0

cin >> num1; 

您可以檢查,如果其SENTIEL數和從迴路斷線:

if (num1 == SENTIEL) 
    break 

,這樣就不會執行其他CIN的。

1

使用break;指令是一種可能性,但通常被認爲不夠優雅,難以維護。

原因是你打破了代碼的流程,你的程序的讀者(這可能不是你在大的程序中,或者如果你分享它)期望你的循環代碼繼續到它。如果你的函數有一個或幾個指令,可能很難理解函數的意圖,因爲你必須記住在哪些情況下循環已經結束。

解決你的問題的基本原則是在得到num1的值之後,在正確的地方開始你的循環。那麼你的循環也必須以它結束。這樣,在用戶鍵入num1後,您將始終檢查退出條件。

// initialize the count and sum 
count = 0; 
sum = 0; 

// Prompt user for the first number or to quit 
cout << "If you want to quit enter " << SENTINEL << " to stop\n " << endl; 
cout << "Enter first number "; 
cin >> num1; 

while (num1 != SENTINEL) 
{ 

    // Prompt the user for the second number 
    cout << "please enter second number     "; 
    cin >> num2; 

    // Prompt the user for a third number 
    cout << "please enter the third number    "; 
    cin >> num3; 

    // Compare numbers 1 and 2 for the largest number 
    if (num1 > num2) 
    { 
     largest = num1; 
    } 
    else 
    { 
     largest = num2; 
    } 

    // Compare largest to last number input 
    if (num3 > largest) 
    { 
     largest = num3; 
    } 

    // Display the largest number 
    cout << "largest number is: " << largest << endl; 

    // Increment the count 
    count++; 

    // Prompt user for the first number or to quit 
    cout << "If you want to quit enter " << SENTINEL << " to stop\n " << endl; 
    cout << "Enter first number "; 
    cin >> num1; 
} 

if (count > 0) 
{ 
    average = sum/count; 
    cout << "Average of the numbers is " << average; 
} 

return 0; 

注:我這裏沒有解決代碼錯誤,如在評論中提到的(變量沒有被宣佈,合計不被更新,等等)。

+0

1.您正在複製代碼(那3行出現在循環之前和每次迭代結束時),這比「break」更不優雅(反正什麼都不優雅?)。 2.你忘了這個程序實際工作所需的一小部分(提示:'sum'與它有關)。 – 2014-09-12 19:26:20

+0

複製代碼實際上並不優雅,但我不會說「break」。 break指令的問題在於它打破了代碼流。在一個小程序中,它可能不是非常重要,但是你希望循環從開始到結束。不得不尋找其他可能的循環終止使得代碼難以閱讀,並且在很多公司中被禁止或者真的不被讚賞。 – Mitvailer 2014-09-12 19:32:27

+0

感謝您的幫助! – RJH 2014-09-12 21:07:30

相關問題