2016-11-09 6 views
0

こんにちは皆、私はループに入力される数字のランダムな量の平均を求めようとしています。私は奇妙な答えを得る平均を見つけるためにしようとするが、私は右の合計を印刷することができるループの後に合計の理由のために。誰もがこれで私を助けることができるか、ここにスレッドに役立つことができますか?私はここで何かを見つけることができませんでした。C++のループで行われた合計の平均を求める

ここでは動作しないプログラムのコードです。

#include <iostream> 
#include <fstream> 
#include <string> 

using namespace std; 

int main() 
{ 
ifstream inData; 
string golferFile; 
string golfer; 
int matches; 
int score; 

cout << endl; 
cout << "Enter the golfer's filename: "; 
cin >> golferFile; 
inData.open(golferFile.c_str()); 
getline(inData, golfer); 
inData >> matches; 
cout << endl; 
cout << "The golfer " << golfer << " has " << matches << " matches with   scores" 
      " of" << endl; 
cout << endl; 
int count; 
count = 1; 
int matchNumber; 
matchNumber = 1; 
int sum; 
while(count <= matches) 
    { 
    inData >> score; 
    cout << "Match " << matchNumber << ": " << score << endl; 
    matchNumber++; 
    count++; 
    sum = sum + score; 
    } 
    } 
int mean; 
mean = sum/matches; 
cout << "The mean score is " << mean << endl; 
return 0; 
} 

私が受け取る出力は、これは私があなたのコード内のいくつかのエラーを検出しました平均スコアは1399255

+1

あなたはsum変数を初期化しますか? –

+0

ループの前にゼロに初期化する必要がありますか?私は今すぐループの中で初期化しました – John

+0

はいループ外のゼロで初期化します。 –

答えて

0

ある平均

ためです。

  • sum変数を初期化するのは忘れています。 whileループあなたはループの実行が無限にも、あなたのループを初期化time.soなぜ.thatあなたloopを停止するには何も書いていない
  • それfound.remove余分なブレースで
関連する問題