2017-01-23 13 views
-4

私は射手の得点を求めるネストループコードを作成する必要があります。四つの射手と三つの射手があるので、射手は射手のために別の3つの値。問題は、アーチェルの得点を求め続けているだけで、次のラウンドには行かず、現在のラウンドを終了させず、そのラウンドの平均得点を表示しないということです。C++代入アシスタンス

#include <iostream> 
using namespace std; 

int main() 
{ 

    //DECLARATIONS 
    int score; 
    int round; 
    int total; 
    double average = 0; // average score of an archer 

    for (round = 0; round < 4;) { 
     cout << "Please enter the Archer's Score' "; 
     cin >> score; 
     if (score<0, score> 60) { 
      cout << "\nThe value you entered is out of range, Please enter a number between 0 - 60 \n"; 
     } 
     total = total + score; 
    } 

    cout << "Total Score = " << total << endl; 
    average = total/round; 
    cout << "Average Score = " << average << endl; 
    return 0; 
} 
+3

'if(スコア<0, score> 60){'はあなたの望むことをしません。 – drescherjm

+0

合計は初期化されていません。たぶん、あなたは増加ラウンドする必要があります。 – drescherjm

+3

あなたは 'round'をインクリメントしません。 ** – Beta

答えて

0

これはいかがですか? "forループ"の変数ラウンドは決して増加しません。

#include <iostream> 
using namespace std; 

int main() 
{ 

    //DECLARATIONS 
    int score; 
    int round; 
    int total; 
    int count; 
    double average = 0; // average score of an archer 

    for (round = 0; round < 4; round++) { 
     total = 0; 
     for(count = 0; count < 3;) 
     { 
      cout << "Please enter the Archer's Score' "; 
      cin >> score; 
      if (score<0 || score> 60) { 
       cout << "\nThe value you entered is out of range, Please enter a number between 0 - 60 \n"; 
      } 
      else 
      { 
       count++; 
       total = total + score; 
      } 
     } 

     cout << "Total Score = " << total << endl; 
     average = total/count; 
     cout << "Average Score = " << average << endl; 
    } 
    return 0; 
} 
0

roundを見て、for状態を取ります。ラウンド変数を正しく初期化し、ループの終了条件を正しく設定しましたが、その条件を満たすことに問題があります。どのようにしてラウンド変数を終了条件に一致させることができますか?

0
for (round = 0; round < 4;) 

このループのためのラウンド変数がインクリメントされることはありませんので、無限です。

if (score < 0 || score > 60) 

エラーメッセージが表示されますが、間違ったスコアを置き換えるために別のスコアを入力することはできません。

関連する問題