Write a program that reads student\'s names followed by their test scores The pr
ID: 3675751 • Letter: W
Question
Write a program that reads student's names followed by their test scores The program should output each student's name followed by die test scores aisd the relevant grade. It should alto 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 which has four components: studentFName and atudor.studentLName of type string, test Score of type int (tent Score is between 0 and 100), and grade of type chat. Suppose tlut the class has 20 students. Use an amy of 20 components of type studentType.Explanation / Answer
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
typedef struct
{
string studentFName;
string studentLName;
int testScore;
string grade;
}
studentType;
const int MAX_STUDENTS = 10;
const char* STUDENTS_FILE = "students.dat";
const int readStudents(istream& input, studentType students[], const int maxStudents);
void calculateGrades(studentType students[], const int nStudents);
const int findHighestScore(const studentType students[], const int nStudents);
void reportTopScores(const int highestScore, const studentType students[], const int nStudents);
const char getLetterGrade(const int grade);
void trim(string& str);
int main()
{
int exitStatus = 0;
fstream inputFile(STUDENTS_FILE, fstream::in);
if (!inputFile.is_open())
{
cerr << "unable to open file" << endl;
exitStatus = 1;
}
else
{
studentType students[MAX_STUDENTS];
const int nStudents = readStudents(inputFile, students, MAX_STUDENTS);
inputFile.close();
calculateGrades(students, nStudents);
const int highestScore = findHighestScore(students, nStudents);
reportTopScores(highestScore, students, nStudents);
}
return exitStatus;
}
const int readStudents(istream& input, studentType students[], const int maxStudents)
{
int student = 0;
int lineNo = 0;
do
{
string lineBuffer;
getline(input, lineBuffer);
lineNo++;
if (lineBuffer.empty())
{
continue;
}
else
{
string::size_type n = 0;
string::size_type found = lineBuffer.find_first_of(',');
if (found == string::npos)
{
cerr << "invalid student data at line #" << lineNo << " : " << lineBuffer << endl;
continue;
}
else
{
students[student].studentFName = lineBuffer.substr(n, found);
trim(students[student].studentFName);
}
n = found + 1;
found = lineBuffer.find_first_of(',', n);
if (found == string::npos)
{
cerr << "invalid student data at line #" << lineNo << " : " << lineBuffer << endl;
continue;
}
else
{
students[student].studentLName = lineBuffer.substr(n, found - n);
trim(students[student].studentLName);
}
n = found + 1;
students[student].testScore = atoi(lineBuffer.substr(n).c_str());
student++;
}
}
while (!input.eof() && student < maxStudents);
return student;
}
void calculateGrades(studentType students[], const int nStudents)
{
for (int student = 0; student < nStudents; student++)
{
students[student].grade = getLetterGrade(students[student].testScore);
}
}
const int findHighestScore(const studentType students[], const int nStudents)
{
int highestScore = 0;
for (int student = 0; student < nStudents; student++)
{
highestScore = max(highestScore, students[student].testScore);
}
return highestScore;
}
void reportTopScores(const int highestScore, const studentType students[], const int nStudents)
{
cout << endl<< "Top Score for Class of " << nStudents << " Students is "<< highestScore << " (" << getLetterGrade(highestScore) << ")"<< endl;
for (int student = 0; student < nStudents; student++)
{
if (students[student].testScore == highestScore)
{
cout << students[student].studentLName << ", "
<< students[student].studentFName
<< endl;
}
}
}
const char getLetterGrade(const int grade)
{
char letterGrade;
if (grade >= 90)
{
letterGrade = 'A';
}
else if (grade >= 80)
{
letterGrade = 'B';
}
else if (grade >= 70)
{
letterGrade = 'C';
}
else if (grade >= 60)
{
letterGrade = 'D';
}
else
{
letterGrade = 'F';
}
return letterGrade;
}
void trim(string& str)
{
string::size_type pos = str.find_last_not_of(" ");
if (pos != string::npos)
{
str.erase(pos + 1);
pos = str.find_first_not_of(" ");
if (pos != string::npos)
{
str.erase(0, pos);
}
}
else
{
str.erase(str.begin(), str.end());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.