Write a program that reads a student's name followed by their test score. The program should output each students 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.
Student data should be stored in a struct variable of type studentType, which has four components: studentFName and studentLName of type string, testscore of type int (testscore is between 0 and 100), and grade of type char. Suppose that the class has 20 students. Use an array of 20 components of type studentType.
The program must contain at least the following functions:
a.) a function to read the students data into the array.'
b.) a function to assign the relevant grade to each student.
c.) a function to find the highest test score.
d.) a function to print the names of the students having the highest test score.
The program must output each students name in this form: last name followed by a comma, followed by a space, followed by the 1st name, and the name must be left-justified. Moreover, other than declaring the variables and opening the input and output files, the function main should only be a collection of functions calls.
I am really stuck on this and would like some help. I have a mac and am using codeblocks. My issue is that it compiles, but it keeps stopping at finding the data file.
Code:
#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(ifstream& inData, studentType sList[], int listSize);
void calculateGrade(studentType sList[], int listSize);
int highestScore(const studentType sList[], int listSize);
void printResult(ofstream& outData, const studentType sList[], int listSize);
int main()
{
ifstream inData;
ofstream outData;
studentType studentList[NO_OF_STUDENTS];
inData.open("Data.txt");
if (!inData)
{
cout << "The input file does not exist."
<< endl;
system("PAUSE");
return 1;
}
outData.open("DataOut.txt");
if (!outData)
{
cout << "Could not open the output file."
<< endl;
system("PAUSE");
return 1;
}
getData(inData, studentList, NO_OF_STUDENTS);
calculateGrade(studentList, NO_OF_STUDENTS);
printResult(outData, studentList, NO_OF_STUDENTS);
system("PAUSE");
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].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 << 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 << "The higest test score is: " << maxScore << endl;
outFile << "Students the have the higest test scores are:" << endl;
for (i = 1; i < listSize; i++)
if (sList[i].testScore == maxScore)
outFile << sList[i].studentLName + ", " + sList[i].studentFName << endl;
}
main.cppData.txt
You just need to create DataOut.txt file because for some reason I cannot upload/attach the empty file.
Thank you.