I have a program assignment to write a program that reads students’ names followed by their test scores. The program should output each student’s name followed by the test scores and the relevant grade. It should also find and print the highest test score and the name of the students having the highest test score. I use functions and structs, with arrays to accomplish this, and of course the expected InFile mechanics. However, I'm having issues storing the content from the file into the appropriate member, and cannot get anything stored at all.
I've tried rewriting the function and placing it before and after the main function, to no avail, and renaming and restructuring my struct.
My code is as follows.
#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;
}
These are the error codes I'm receiving
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