我班级的程序需要返回平均分数,具有姓氏和名字的高分以及低分。我发现查找高分和低分名称的函数时出错,但我不确定是什么问题。
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
float highestVal(float highScore, float score);
float lowestVal(float lowScore, float score);
string highFirstFun(float highScore, float score, string firstNameHigh);
string highLastFun(float highScore, float score, string lastNameHigh);
string lowFirstFun(float lowScore, float score, string firstNameLow);
string lowLastFun(float lowScore, float score, string lastNameLow);
int main() {
ifstream inFile;
string fileName = "", firstName = "", lastName = "";
float score, average;
float highScore = 0;
float lowScore = 100;
float studentNum = 0;
float sum = 0;
int i;
string highFirst = "", highLast = "";
string lowFirst = "" , lowLast = "";
cout << "Please enter the input file name: ";
cin >> fileName;
inFile.open(fileName);
while (!inFile.is_open()) {
cout << "Sorry, the file did not open. \nTry again, or type \"cancel\" to quit: ";
cin >> fileName;
if (fileName == "cancel") {
cout << "Cancelling..." << endl;
break;
}
else {
inFile.open(fileName);
}
}
if (inFile.is_open()) {
cout << "The scores are as follows: \n";
while (!inFile.eof()) {
inFile >> firstName >> lastName >> score;
cout << firstName << " " << lastName << " scored " << score << endl;
sum += score;
studentNum++;
highScore = highestVal(highScore, score);
lowScore = lowestVal(lowScore, score);
highFirst = highFirstFun(highScore, score, firstName);
highLast = highLastFun(highScore, score, lastName);
lowFirst = lowFirstFun(highScore, score, firstName);
lowLast = lowLastFun(highScore, score, lastName);
}
average = (sum / studentNum);
cout << "There are " << studentNum << " students with an average grade of " << average << endl;
cout << "The high score was: " << highScore << " from " << highFirst << " " << highLast << endl;
cout << "The low score was: " << lowScore << " from " << lowFirst << " " << lowLast << endl;
inFile.close();
}
return 0;
}
float highestVal(float highScore, float score)
{
if (score > highScore) {
highScore = score;
}
return (highScore);
}
float lowestVal(float lowScore, float score)
{
if (score < lowScore) {
lowScore = score;
}
return (lowScore);
}
string highFirstFun(float highScore, float score, string firstNameHigh)
{
if (score > highScore) {
return (firstNameHigh);
}
}
string highLastFun(float highScore, float score, string lastNameHigh)
{
if (score > highScore) {
return (lastNameHigh);
}
}
string lowFirstFun(float lowScore, float score, string firstNameLow)
{
if (score < lowScore) {
return (firstNameLow);
}
}
string lowLastFun(float lowScore, float score, string lastNameLow)
{
if (score < lowScore) {
return (lastNameLow);
}
}
这里是从中提取的名为“ Scores.txt”的文本文件:
John Smith 99.0
Sarah Johnson 85.0
Jim Robinson 70.0
Mary Anderson 100.0
Michael Jackson 92.0
任何帮助将不胜感激!
具有返回类型的函数必须始终返回该类型。函数
string highFirstFun(float highScore, float score, string firstNameHigh);
例如,在调用string
时必须始终返回。问题是您的功能的定义
string highFirstFun(float highScore, float score, string firstNameHigh)
{
if (score > highScore) {
return (firstNameHigh);
}
}
highFirstFun()
仅在[且仅当string
高于score
时才返回highScore
。好吧,如果分数不高于highScore会怎样?这对于该函数将是一个问题,因为如果发生这种情况,它不会做任何事情,因此编译器会抱怨它。这适用于您的所有功能。为了使它们起作用,您必须找到一种在所有情况下都使它们返回字符串的方法。