Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(for c++) Write a program that reads students’ names followed by their test scor

ID: 3763420 • Letter: #

Question

(for c++)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 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.

Turn in your source code.

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());
}
}