所以我遇到了Void printresult功能的问题 我无法让它工作编译器错误我得到的是outFile未声明和最高分“明显调用的括号前面的表达式必须具有(指针 - ) - 函数类型。 一些方向将不胜感激。
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
const int NO_OR_STUDENTS = 20;
struct studentType
{
string studentFName;
string studentLName;
int testScore;
char grade;
};
void getData(ifstream& inFile, studentType sList[], int listSize);
void calculateGrade(studentType sList[], int listSize);
int highestScore(const studentType sList[], int listSize);
void printResult(ofstream& outFile, const studentType sList[], int listSize);
int main()
{
ifstream inData;
ofstream outData;
studentType studentList[NO_OR_STUDENTS];
inData.open("Ch9_Ex2Data.txt");
if(!inData)
{
cout << "The input file does not exist. Program terminates!"
<< endl;
system("PAUSE");
return 1;
}//endif
outData.open("Ch11_Ex1Out.txt");
if(!outData)
{
cout << "Cannot open the output file. Program terminates!"
<< endl;
system("PAUSE");
return 1;
}//endif
getData(inData, studentList, NO_OR_STUDENTS);
calculateGrade(studentList, NO_OR_STUDENTS);
printResult(outData, studentList, NO_OR_STUDENTS);
system("PAUSE");
return 0;
}//endmain
void getData(ifstream& inFile, studentType sList[], int listSize)
{
for(int i = 0; i < listSize; i++)
inFile >> sList[i].studentFName >> sList[i].studentLName
>> sList[i].testScore;
}//endgetData
void calculateGrade(studentType sList[], int listSize)
{
int score;
for(int i = 0; i < listSize; i++)
if(score >= 90)
sList[i].testScore = 'A';
else if(score >= 80)
sList[i].testScore = 'B';
else if(score >= 70)
sList[i].testScore = 'C';
else if(score >= 60)
sList[i].testScore = 'D';
else
sList[i].testScore = 'F';
//student writes code for this function
}//endcalculateGrade
int highestScore(const studentType sList[], int scores[], int listSize) //orignal parameters only had one array and an int
//must pass in the scores array otherwise the program has no idea what it is inside the function
{
int highestScore = scores[0];
for(int i = 0; i < listSize; i++)
{
if(scores[i] > highestScore)
{
highestScore = scores[i];
}
}
}
void printResult(ofstream& outFile, const studentType sList[], int listSize)
{
int maxScore;
int maxScore = highestScore(sList, listSize);
int i;
outFile << setw(15) << "Student Name "
<< setw(10) << "Test Score"
<< setw(7) << "Grade" << endl;
for(i = 1; i < listSize; i++)
outFile << left << setw(25)
<< sList[i].studentLName + ", " + sList[i].studentFName
<< right << " " << setw(5) << sList[i].testScore
<< setw(6) << " " << sList[i].grade << endl;
outFile << endl << "Highest Test Score: " << maxScore << endl;
outFile << "Students having the highest test score:" << endl;
for(i = 1; i < listSize; i++)
if(sList[i].testScore == maxScore)
outFile << sList[i].studentLName + ", " + sList[i].studentFName << endl;
}//endprintResult
答案 0 :(得分:2)
最可能的罪魁祸首是这两个函数的函数定义末尾的分号。将void printResult(ofstream& outFile, const studentType sList[], int listSize);
更改为void printResult(ofstream& outFile, const studentType sList[], int listSize)
,同样更改为最高分数,看看是否有更好的效果。
编辑:还有两个问题(在评论中找到):您在highestScore()
函数中忘记了for循环中的右括号,之后需要return highScore;
。
此外,您声明了该函数的两个版本 - 我不确定这是否是复制粘贴错误,或者您的代码中实际上有两个版本,但无论它是什么,请删除其中一个!< / p>
答案 1 :(得分:1)
highestScore
的第一个版本并未以大括号}
正确结束。
答案 2 :(得分:1)
当我尝试自己编译代码时,我只发现了2个语法错误:
您已在同一范围内(在'printResult()'中)声明了'maxscore'两次。删除您未初始化的第一个声明。
'highestScore'不会返回任何内容。
在修复这些代码之后,你的代码应该编译,虽然'calculateGrade()'中的'score'没有被初始化,所以它不会像你期望的那样动作。
注意所产生的任何警告,大多数警告实际上最终会导致代码中的错误,因此修复它们将有助于最大限度地减少错误。