Compiler Error, Help Needed

The errors I'm getting are:
1) error: 'highestScore' cannot be used as a function:

int highestScore = scores[0];


2) error: 'outFile' was not declared in this scope:

outFile << setw(15) << "Student Name"


3) error: expected '}' at end of input:

outFile << sList[i].studentLName + ", " + sList[i].studentFName << endl;
}
}

Can someone figure out what the problem is?
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>

using namespace std;

const int 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[STUDENTS];
inData.open("Ch9_Ex2Data.txt");
if (!inData)
{
cout << "Input file does not exist."
<< endl;

return 1;
}


outData.open("Ch9_Ex2Out.txt");
if (!outData)
{
cout << "Cannot open the output file."
<< endl;

return 1;
}


getData(inData, studentList, STUDENTS);
calculateGrade(studentList, STUDENTS);
printResult(outData, studentList, STUDENTS);

return 0;
}


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].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';
}

int highestScore(const studentType sList[], int scores[], int listSize)
{
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 = 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;
}
}
Function highestScore has several problems.
a) a variable is used which has the same name as the function itself. Change the variable name to something else.
b) the function should return a value. It has no return statement.
c) the prototype declared on line 22 uses a different number of parameters to the function definition further down.
d) it's hard to tell because of the lack of indentation, but it looks like this function has no closing brace.

The definition of function printResult has a semicolon, which should not be there.
Here is my new code, but I have 1 problem:

Although when I compile everything and there are no errors nor warnings, the output doesn't give me anything!

Can someone help me figure this out ASAP!?


#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>

using namespace std;

const int NO_OF_STUDENTS = 20;

struct studentType{
string studentFName;
string studentLName;
int testScore;
char grade;
};

void getData(studentType sList[], int listSize){
ifstream inFile("Ch9_Ex2Data.txt");
int count = 0;

while ( count < listSize){
inFile >> sList[count].studentLName >> sList[count].studentFName >> sList[count].testScore;
count++;
}

inFile.close();
}

void calculateGrade(studentType sList[], int listSize){
int score = 0;
char grade = 'A';

for (int i = 0; i < listSize; i++){
score = sList[i].testScore;

if ( score <= 59 ){
grade = 'F';
}else if ( score >= 60 && score <= 69 ){
grade = 'D';
}else if ( score >= 70 && score <= 79 ){
grade = 'C';
}else if ( score >= 80 && score <= 89 ){
grade = 'B';
}else{
grade = 'A';
}
sList[i].grade = grade;
}
}

int highestScore (const studentType sList[], int listSize)
{
int max = sList[0].grade;
for (int i = 0; i < listSize; i++) {
if (sList[i].grade > max)
max = sList[i].grade;
}
return max;
}

void printResult(const studentType sList[], int listSize){
ofstream outFile("Ch9_Ex2Out.txt");
string name = "";

outFile << left << setw(30) << "Student Name" << right << setw(10) << "Test Score" << right << setw(7) << "Grade" << endl;

for (int i = 0; i < listSize; i++){
name = sList[i].studentLName + ", " + sList[i].studentFName;

outFile << left << setw(30) << name << right << setw(10) << sList[i].testScore << right << setw(7) << sList[i].grade << endl;
}

outFile << endl;

int highscore = highestScore(sList, listSize);

outFile << "Highest Test Score: " << highscore << endl;
outFile << "Students having the highest test score: " << endl;

for (int i = 0; i < listSize; i++){
if ( sList[i].testScore == highscore ){
outFile << sList[i].studentLName << ", " << sList[i].studentFName << endl;
}
}

outFile.close();
}

int main(){
studentType sList[NO_OF_STUDENTS];

getData(sList, (int) NO_OF_STUDENTS);
calculateGrade(sList, (int) NO_OF_STUDENTS);
printResult(sList, (int) NO_OF_STUDENTS);
system("pause");
return 0;
}
Last edited on
Although when I compile everything and there are no errors nor warnings, the output doesn't give me anything!

While we are on the topic of errors and warnings, what happens if either the input or output file is not opened successfully?

You should always as a matter of routine check for successful opening of files. Use file.good() or file.is_open() to check for this condition.
If there's a problem, output a useful message to show what went wrong, and exit.

A different topic. Function highestScore()
Here you assign char grade to int max. I think that is a mistake.
Last edited on
Topic archived. No new replies allowed.