我有一个程序作业,要编写一个程序,以读取学生的姓名和他们的考试成绩。该程序应输出每个学生的姓名,然后输出考试成绩和相关成绩。它还应该找到并打印最高的考试成绩以及具有最高考试成绩的学生的姓名。我使用带有数组的函数和结构来完成此操作,当然还要使用预期的InFile机制。但是,我在将内容从文件存储到适当的成员时遇到了问题,根本无法存储任何内容。
我尝试重写该函数并将其放置在main函数之前和之后,但无济于事,并重命名和重组了我的struct。
我的代码如下。
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
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() {
//Variable Declaration
const int STUDENTCOUNT = 20;
ifstream inData;
ofstream outData;
studentType studentList[STUDENTCOUNT];
//Open file
inData.open("Ch9_Ex2Data.txt");
//Call functions
getData(inData, studentList, STUDENTCOUNT);
calculateGrade(studentList, STUDENTCOUNT);
printResult(outData, studentList, STUDENTCOUNT);
system("PAUSE");
return 0;
}
//Data from Ch9_Ex2Data.txt function
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;
}
void calculateGrade(studentType sList[], int listSize)
{
int score;
for (int i = 0; i < listSize; i++)
if (score >= 90)
sList[i].grade() = 'A';
else if (score >= 80)
sList[i].grade() = 'B';
else if (score >= 70)
sList[i].grade() = 'C';
else if (score >= 60)
sList[i].grade() = 'D';
else
sList[i].grade() = 'F';
}
int highestScore(const studentType sList[], int listSize)
{
int score[100];
int highscore = score[0];
for (int i = 0; i < listSize; i++)
{
if (score[i] > highscore)
{
highscore = score[i];
}
}
}
void printResult(ofstream& outFile, const studentType sList[],
int listSize)
{
int maxScore = highestScore(sList, listSize);
int i;
outFile << "Student Name " << "Test Score" << "Grade" <<
endl;
for (i = 1; i < listSize; i++)
outFile << left << sList[i].studentLName() + ", " +
sList[i].studentFName << right << " " << s
List[i].testScore << " " << 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;
}
i = 1; i < listSize; i++)
if (sList[i].testScore() == maxScore)
outFile << sList[i].studentLName() + ", " +
sList[i].studentFName << endl;
}
这些是我收到的错误代码
main.cpp: In function ‘void getData(std::ifstream&, studentType*, int)’:
main.cpp:42:10: error: invalid use of non-static member function
‘std::__cxx11::string studentType::studentFName()’
inFile >> sList[i].studentFName >> sList[i].studentLName
main.cpp:9:16: note: declared here
string studentFName();
^~~~~~~~~~~~
main.cpp: In function ‘void calculateGrade(studentType*, int)’:
main.cpp:51:27: error: lvalue required as left operand of assignment
sList[i].grade() = 'A';
^~~
main.cpp:53:27: error: lvalue required as left operand of assignment
sList[i].grade() = 'B';
^~~
main.cpp:55:27: error: lvalue required as left operand of assignment
sList[i].grade() = 'C';
^~~
main.cpp:57:27: error: lvalue required as left operand of assignment
sList[i].grade() = 'D';
^~~
main.cpp:59:27: error: lvalue required as left operand of assignment
sList[i].grade() = 'F';
^~~
main.cpp: In function ‘int highestScore(const studentType*, int)’:
main.cpp:73:5: warning: no return statement in function returning non-void [-Wreturn-type]
}
^
main.cpp: In function ‘void printResult(std::ofstream&, const studentType*, int)’:
main.cpp:82:45: error: passing ‘const studentType’ as ‘this’ argument discards qualifiers [-fpermissive]
outFile << left << sList[i].studentLName() + ", " + sList[i].studentFName << right << " " << sList[i].testScore << " " << sList[i].grade << endl;
^
main.cpp:10:16: note: in call to ‘std::__cxx11::string studentType::studentLName()’
string studentLName();
^~~~~~~~~~~~
main.cpp:82:54: error: invalid use of non-static member function ‘std::__cxx11::string studentType::studentFName()’
outFile << left << sList[i].studentLName() + ", " + sList[i].studentFName << right << " " << sList[i].testScore << " " << sList[i].grade << endl;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
main.cpp:9:16: note: declared here
string studentFName();
^~~~~~~~~~~~
main.cpp:86:27: error: passing ‘const studentType’ as ‘this’ argument discards qualifiers [-fpermissive]
if (sList[i].testScore() == maxScore)
^
main.cpp:11:13: note: in call to ‘int studentType::testScore()’
int testScore();
^~~~~~~~~
main.cpp:87:38: error: passing ‘const studentType’ as ‘this’ argument discards qualifiers [-fpermissive]
outFile << sList[i].studentLName() + ", " + sList[i].studentFName << endl;
^
main.cpp:10:16: note: in call to ‘std::__cxx11::string studentType::studentLName()’
string studentLName();
^~~~~~~~~~~~
main.cpp:87:47: error: invalid use of non-static member function ‘std::__cxx11::string studentType::studentFName()’
outFile << sList[i].studentLName() + ", " + sList[i].studentFName << endl;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
main.cpp:9:16: note: declared here
string studentFName();
^~~~~~~~~~~~
/bin/bash: line 4: ./a.out: No such file or directory
答案 0 :(得分:1)
struct studentType
{
string studentFName(); // function declaration
string studentLName();// function declaration
int testScore();// function declaration
char grade();// function declaration
};
您刚刚在结构中声明了4个方法。应该是:
struct studentType
{
string studentFName;
string studentLName;
int testScore;
char grade;
};