我被分配去做一個需要6分的程序,下降最低的分數然後取最高的5分的平均值。我不明白爲什麼我無法在calcAverage函數中正確使用最低變量。測試成績演示C++
#include <iostream>
#include <iomanip>
using namespace std;
void getScore(int &Score);
void calcAverage(int, int, int, int, int, int, int &lowest);
int findLowest(int s1, int s2, int s3, int s4, int s5, int s6, int &lowest);
int main()
{
int s1, s2, s3, s4, s5, s6, lowest;
// Call on getScore function to iterate through each of the six scores
getScore(s1);
getScore(s2);
getScore(s3);
getScore(s4);
getScore(s5);
getScore(s6);
// Call the calcAverage to display average of five highest scores
calcAverage(s1, s2, s3, s4, s5, s6, lowest);
system("pause");
return 0;
}
void getScore(int &Score)
{
cout << "Please enter a test score (Value betweent 1 and 100): ";
cin >> Score;
// Validate the score input
while (Score < 1 || Score > 100)
{
cout << "Invalid input, Please enter a test score value from 0 to 100! ";
cin >> Score;
}
}
int findLowest(int s1, int s2, int s3, int s4, int s5, int s6, int lowest)
{
lowest = s1;
if (s2 < lowest)
lowest = s2;
else if (s3 < lowest)
lowest = s3;
else if (s4 < lowest)
lowest = s4;
else if (s5 < lowest)
lowest = s5;
else if (s6 < lowest)
lowest = s6;
cout << "The lowest test score is: " << lowest << endl;
return lowest;
}
void calcAverage(int s1, int s2, int s3, int s4, int s5, int s6, int &lowest)
{
int findLowest(int, int, int, int, int, int, int);
double avg;
findLowest(s1, s2, s3, s4, s5, s6, lowest);
avg = ((s1 + s2 + s3 + s4 + s5 + s6) - lowest)/5.0;
cout << fixed << showpoint << setprecision(2);
cout << "The average of the five highest scores is: " << avg << endl;
}
想想看,如果's1'碰巧是最高的成績。 – Escualo
你的'findLowest()'函數在許多情況下會失敗。你需要擺脫'else'才能使它正常工作。 – NathanOliver
注意:找到最低值是'最低= std :: min({s1,s2,s3,s4,s5,s6});'(C++ 11) –