2016-10-07 11 views
0

私は平均スコアとして私にtest1/5を与え続けます。何が間違っているのかわかりません。 Iveは()を別の方法でグループ化しようとしました。私はそれがちょうど終了した一握りの時間を働いたことを誓う。今はほぼ1amと私はまだこの単純なコードを把握しようとしています。なぜ簡単な数学が使えないのですか?

たとえば:60、50、80、70、80は私の12の平均を与える(TEST1/5)

#include <iostream> 
#include <string> 


using namespace std; 

double testAvgCal(double, double, double, double, double, double&); 

void printing(double); 


int main() 
{ 
    double test1 = 0; 
    double test2 = 0; 
    double test3 = 0; 
    double test4 = 0; 
    double test5 = 0; 
    double testAvg = 0; 

    cout << "Enter five test scores separated by commas: " << endl; 
    cin >> test1 >> test2 >> test3 >> test4 >> test5; 


    testAvgCal(test1, test2, test3, test4, test5, testAvg); 


    printing(testAvg); 

} 

double testAvgCal(double test1, double test2, double test3, double test4, double test5, double& testAvg) 
{ 
    testAvg = (test1 + test2 + test3 + test4 + test5)/ 5; 

    return testAvg; 

} 

void printing(double testAvg) 
{ 
    cout << "The test score average of this student is: " << testAvg << endl; 

} 
+0

間違っている正確にどのような?結果は間違っていますか?もしそうなら、[here](http://stackoverflow.com/q/588004/5647260)を参照してください。 – Li357

+0

「0。###」と表示されますか? –

+0

Iveは10の倍数で試してみましたが、いつかは必要になると思います。 –

答えて

8

はここにあなたの問題です:

cout << "Enter five test scores separated by commas: " << endl; 
cin >> test1 >> test2 >> test3 >> test4 >> test5; 

コードがありませんコンマを読んでください。入力演算子>>は、のスペースであるを区切ります。

これは、入力が最初の数字を読み取り、次に別の数字を期待しますが、代わりにカンマを表示して失敗し、何も読み取らないことを意味します。

だから、簡単な解決策は、命令の出力を変更することが本当にある:

cout << "Enter five test scores separated by space: " << endl; 
//           ^^^^^ 
//         Changed here 
+1

ありがとうございました。それは間違いなくそれだった。私はそれを考えなかったでしょう。 –

2

The answer by Joachim Pileborgは正しく、問題を診断し、問題の一つの解決策を提供しています。あなたはカンマ区切りする入力を維持することを決定した場合は、使用することができます:上記

char dummy; 

cout << "Enter five test scores separated by commas: " << endl; 
cin >> test1 >> dummy >> test2 >> dummy >> test3 >> dummy >> test4 >> dummy >> test5; 
1

の答えが正しい、

cin>> test1 >> test2 >> test3 >> test4 >> test5; 

はコンマを読んでいないだろうし、次の値を入力するスペースが必要になります。上記の提案に

別ソリューション:

double total = 0; 
double testScore; 
int totalNuberOfTests = 5;  //can be changed to whatever you want 

for (int i = 0; i< totalNuberOfTests ; i++) 
{ 
    cout<<"Eneter score for Test # "<<i+1<<": "; 
    cin>>testScore; 
    total += testScore; 
} 

double testAverage = total/totalNuberOfTests; 
関連する問題