C ++如何解决这个问题

时间:2014-07-02 18:51:40

标签: c++

#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;

// Function prototype
void getScore(int& score, ifstream& inFile);
void calcAverage(int testScr1, int testScr2, int testScr3, int testScr4, int testcr5);
int findLowest(int testScr1, int testScr2, int testScr3, int testScr4, int testScr5);
int grades(int testScr1, int testScr2, int testScr3, int testScr4, int testScr5);
int main()
{
ifstream inFile;
inFile.open("grades.txt");
int lowest = 100;
int testScr1, testScr2, testScr3, testScr4, testScr5;


getScore(testScr1, inFile);
if (testScr1 < lowest)
{
    lowest = testScr1;
}
getScore(testScr2, inFile);
if (testScr2 < lowest)
{
    lowest = testScr2;
}
getScore(testScr3, inFile);
 if (testScr3 < lowest)
{
     lowest = testScr3;
}
getScore(testScr4, inFile);
 if (testScr4 < lowest)
{
    lowest = testScr4;
}
getScore(testScr5, inFile);
 if (testScr5 < lowest)
{
    lowest = testScr5;
}
calcAverage(testScr1, testScr2, testScr3, testScr4, testScr5);

inFile.close();

return lowest;
}

void getScore(int& score, ifstream& inFile)
{
inFile >> score;
}


void calcAverage(int testScr1, int testScr2, int testScr3, int testScr4, int testScr5)
{
int sum = 0;
int lowest;
double average;

lowest = findLowest(testScr1, testScr2, testScr3, testScr4, testScr5);

sum = testScr1 + testScr2 + testScr3 + testScr4 + testScr5 - lowest;
average = sum / 4.0;

cout << setw(4) << fixed << showpoint << setprecision(2);
cout << "The avergae of the four highest scores are: " << average << endl;
}

int findLowest(int testScr1, int testScr2, int testScr3, int testScr4, int testScr5)
{
int lowest = testScr1;

cout << "The lowest test score is: " << lowest << endl;

return lowest;
}

这是我的问题..这个程序读取一个包含5个等级的txt文件。它假设返回最低等级,然后报告它似乎做的4个剩余等级的平均值。我遇到的问题是,在底部,我很确定我告诉程序回读testScr1始终是最低的。这不是我的愿望,我很确定这是最后一个问题。我不知道如何解决这个问题。我是编程新手,这就是为什么代码可能看起来不是世界上最好的,解决方案可能很简单,但我没有看到它。

1 个答案:

答案 0 :(得分:3)

你真的应该使用调试器来逐步完成你的代码,这将有助于你验证你的怀疑。

然而,你的直觉是正确的。您始终将testScr1分配给lowest。您在阅读5个等级时已经计算了lowest的值,因此可以放弃该部分并修复findLowest()或放弃findLowest()