In: Computer Science
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. 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. Your program must contain at least the following functions:
Your program must output each student’s name in this form: last name followed by a comma, followed by a space, followed by the first name; 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 function calls.
This should be written in C++ please thank you.
Data must be read from a file
Code is provided as text at last
Sample "Student.dat" file-
Program-
Output-
Code-
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include <cstdlib>
#include <sstream>
using namespace std;
//Define structure studentType
struct studentType
{
//Declaring variables
string studentFirstName;
string studentMiddleName;
string studentLastName;
double testScore;
char grade;
};
//Functions required
void getData(ifstream& inFile, studentType sList[],
int listSize);
void calculateGrade(studentType sList[], int listSize);
int highestScore(const studentType sList[], int listSize);
void printResult(const studentType sList[], int
listSize);
//Define main function
int main()
{
//Defining in variable
ifstream in;
//Open student file
in.open("student.dat");
//If file can't be opened
if (in.fail())
{
//Display message
cout << "file did not open please check it\n";
//Pause console window
system("pause");
//Exit
system("exit");
}
//Define array
studentType sList[20];
//Call "getData()"
getData(in, sList, 20);
//Call "calculateGrade()"
calculateGrade(sList, 20);
//Call "printResult()"
printResult(sList, 20);
//Close
in.close();
//Pause console window
system("pause");
//Return 0
return 0;
}
//Define getData()
void getData(ifstream& inFile, studentType sList[],
int listSize)
{
//Declare variables
int n = 0;
//Loop until list size
while (n<listSize)
{
//Store data
string lineOfFile = "";
getline(inFile, lineOfFile);
istringstream
iss(lineOfFile);
if (lineOfFile != "") {
int numWord =
1;
do
{
std::string sub;
iss >> sub;
if (numWord == 1)
sList[n].studentFirstName =
sub;
else if (numWord == 2)
sList[n].studentMiddleName =
sub;
else if (numWord == 3)
sList[n].studentLastName =
sub;
numWord++;
//std::cout << "Substring: " << sub
<< std::endl;
} while
(!iss.eof());
//
means only 2 words were entered... example first and last
if (numWord ==
3) {
sList[n].studentLastName =
sList[n].studentMiddleName;
sList[n].studentMiddleName = "";
}
inFile >> sList[n].testScore;
//inFile
>> sList[n].studentLastName >>
sList[n].studentFirstName >> sList[n].testScore;
//Increment counter
n++;
}
}
}
//Define calculateGrade()
void calculateGrade(studentType sList[], int listSize)
{
//Declare variable
int i;
//Loop until length
for (i = 0; i<listSize; i++)
//If score less than 60
if (sList[i].testScore<60)
//Set grade
sList[i].grade = 'F';
//If score less than 70
else if (sList[i].testScore<70)
//Set grade
sList[i].grade = 'D';
//If score less than 80
else if (sList[i].testScore<80)
//Set grade
sList[i].grade = 'C';
//If score less than 90
else if (sList[i].testScore<90)
//Set grade
sList[i].grade = 'B';
//If score greater than 90
else
//Set grade
sList[i].grade = 'A';
}
//Define highestScore()
int highestScore(const studentType sList[], int listSize)
{
//Declare variables
int high = 0, i;
//Loop until length
for (i = 0; i<listSize; i++)
//If high value is less than score
if (high < sList[i].testScore)
//Set score as high
high = sList[i].testScore;
//Return high
return high;
}
//Define printResult()
void printResult(const studentType sList[], int listSize)
{
//Display result
cout << left << setw(30) << "Student Name" << right << setw(10) << "TestScore" << right << setw(7) << "Grade" << endl;
//Declare variables
string name;
int high, i;
//Loop until length
for (i = 0; i<listSize; i++)
{
//Set name
name = sList[i].studentLastName + " " + sList[i].studentMiddleName + " " + sList[i].studentFirstName;
//Display message
cout << left << setw(30) << name << right << setw(10) << sList[i].testScore << right << setw(7) << sList[i].grade << endl;
}
//Set new line
cout << endl;
//Call highestScore()
high = highestScore(sList, listSize);
//Display message
cout << "Highest Test Score: " << high << endl;
//Display result
cout << "Students having the highest test score: " << endl;
//Loop until length
for (int i = 0; i < listSize; i++)
//If score is high
if (sList[i].testScore == high)
//Display student name
cout << sList[i].studentLastName << " " << sList[i].studentMiddleName << " " << sList[i].studentFirstName << endl;
}