I am working on the following code, however I cannot figure out how to find the highest score and how to get it to output as well as the name that goes with it. For some reason for highest score I get zero and for students having the highest score I get a blank. Any help would be great!
Code:
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include "RomanHeading.h"
using namespace std;
const int STUDENTS = 24;
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()
{
getHeading("Assignment 10 - Working with Structures");
ifstream inData;
ofstream outData;
studentType studentList[STUDENTS];
inData.open("Ch9_Ex2Data.txt");
if (!inData)
{
cout << "The input file does not exist. Program terminates!"
<< endl;
system("PAUSE");
return 1;
}
outData.open("Ch9_Ex2Out.txt");
if (!outData)
{
cout << "Cannot open the output file. Program terminates!"
<< endl;
system("PAUSE");
return 1;
}
getData(inData, studentList, STUDENTS);
calculateGrade(studentList, STUDENTS);
printResult(outData, studentList, STUDENTS);
getClosing();
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)
{
for (int i = 0; i < listSize; i++)
if (sList[i].testScore >= 90)
sList[i].grade = 'A';
else if (sList[i].testScore >= 80)
sList[i].grade = 'B';
else if (sList[i].testScore >= 70)
sList[i].grade = 'C';
else if (sList[i].testScore >= 60)
sList[i].grade = 'D';
else
sList[i].grade = 'F';
}
int highestScore(const studentType sList[], int listSize)
{
double maxscores = 0;
for (int i = 0; i < listSize; i++)
{
if (sList[i].testScore > maxscores)
{
maxscores = sList[1].testScore;
}
}
return 1;
}
void printResult(ofstream& outFile, const studentType sList[], int listSize)
{
double maxscores = 0;
int i = 0;
cout << setw(15) << "\n\n Student Name "
<< setw(20) << " Test Score"
<< setw(7) << " Grade" << endl;
cout << " ------------ " << setw(21) << " ---------- " << setw(7) << " ----- " << endl;
for (i = 1; i < listSize; i++)
cout << left << setw(25)
<< " " + sList[i].studentLName + ", " + sList[i].studentFName
<< right << " " << setw(5) << sList[i].testScore
<< setw(8) << " " << sList[i].grade << endl;
cout << endl << " Highest Test Score: " << maxscores << endl;
cout << " Students having the highest test score:" << endl;
for (i = 1; i < listSize; i++)
if (sList[i].testScore == maxscores)
outFile << sList[i].studentLName + ", " + sList[i].studentFName << endl;
outFile << "\n\n Mayra Roman " << endl;
outFile << " C++ Programming 1 " << endl;
outFile << " Assignment 10 - Working with Structures " << endl;
outFile << setw(15) << "\n Student Name "
<< setw(20) << "Test Score"
<< setw(7) << "Grade" << endl;
outFile << " ------------ " << setw(21) << " ---------- " << setw(7) << " ----- " << endl;
for (i = 1; i < listSize; i++)
outFile << left << setw(25)
<< " " + sList[i].studentLName + ", " + sList[i].studentFName
<< right << " " << setw(5) << sList[i].testScore
<< setw(8) << " " << sList[i].grade << endl;
outFile << endl << " Highest Test Score: " << maxscores << endl;
outFile << " Students having the highest test score:" << endl;
for (i = 1; i < listSize; i++)
if (sList[i].testScore == maxscores)
outFile << sList[i].studentLName + ", " + sList[i].studentFName << endl;
outFile << "\n Program ended " << getDate() << " at " << getTime() << endl;
outFile << " Press the Enter key to continue. " << endl;
}