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

Write a program that reads students’ names file followed by their test scores lo

ID: 3820561 • Letter: W

Question

Write a program that reads students’ names file followed by their test scores located in a txt file (Make up a name for the file, like Student_Names.txt). 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. This program must be compatible with Microsoft Visual Studio as that is the only program we use for coding in this class. Please include minimal comments showing what lines of code are for.

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:
A function to read the students’ data into the array.
A function to assign the relevant grade to each student.
A function to find the highest test score.
A function to print the names of the students having the highest test score.

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 program must be compatible with Microsoft Visual Studio as that is the only program we use for coding in this class. Please include minimal comments showing what lines of code are for.

Explanation / Answer

CODE:

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>

using namespace std;

typedef struct
{
string fName;
string lName;
int scoreObtained;
string grade;
}
studentType;

const int MAX_STUDENTS = 10;
const char* STUDENTS_FILE = "students.dat";

// forward reference for internal functions
const int readFromFile(istream& input, studentType students[], const int maxStudents);
void calculateGrades(studentType students[], const int nStudents);
const int findTopScore(const studentType students[], const int nStudents);
void reportTopScores(const int highestScore, const studentType students[], const int nStudents);
const char getGrade(const int grade);
void trim(string& str);

// main program entry point
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 = readFromFile(inputFile, students, MAX_STUDENTS);
inputFile.close();

calculateGrades(students, nStudents);
  
const int highestScore = findTopScore(students, nStudents);
reportTopScores(highestScore, students, nStudents);
}

return exitStatus;
}

//
// Attempts to read student data from the provided istream and into the provided
// students array.
//
// input - the input stream to read from
// students - the array of student data that will receive the file data
// maxStudents - the maximum size of the students array
//
// returns the actual number of students read from the input stream
//
const int readFromFile(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
{
// look for the comma that separates last name from the first name
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
{
// extract out the last name
students[student].fName = lineBuffer.substr(n, found);
trim(students[student].fName);
}

// look for the comma that separates the first name from the test score
n = found + 1;
found = lineBuffer.find_first_of(',', n);
if (found == string::npos)
{
cerr << "invalid student data at line #" << lineNo << " : " << lineBuffer << endl;
continue;
}
else
{
// extract out the first name
students[student].lName = lineBuffer.substr(n, found - n);
trim(students[student].lName);
}

// extract out the test score from the remaining text
n = found + 1;
students[student].scoreObtained = atoi(lineBuffer.substr(n).c_str());

student++;
}
}
while (!input.eof() && student < maxStudents);
  
return student;
}

// Calculates the grade for all provided student data.
void calculateGrades(studentType students[], const int nStudents)
{
for (int student = 0; student < nStudents; student++)
{
students[student].grade = getGrade(students[student].scoreObtained);
}
}

// Finds the Top test score of all the students.
const int findTopScore(const studentType students[], const int nStudents)
{
int highestScore = 0;
for (int student = 0; student < nStudents; student++)
{
highestScore = max(highestScore, students[student].scoreObtained);
}
return highestScore;
}

// Reports all of the students with the highest test score in the class.
void reportTopScores(const int highestScore, const studentType students[], const int nStudents)
{
cout << endl
<< "Top Score for Class of " << nStudents << " Students is "
<< highestScore << " (" << getGrade(highestScore) << ")"
<< endl;

for (int student = 0; student < nStudents; student++)
{
if (students[student].scoreObtained == highestScore)
{
cout << students[student].lName << ", "
<< students[student].fName
<< endl;
}
}
}

// Calculates the letter grade of a test score.
const char getGrade(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;
}

// To trim the input str for displaying properly
void trim(string& str)
{
// first find any non-whitespace on the right side of the string
string::size_type pos = str.find_last_not_of(" ");
if (pos != string::npos)
{
str.erase(pos + 1);

// then try and find any non-whitespace on the left side of the string
pos = str.find_first_not_of(" ");
if (pos != string::npos)
{
str.erase(0, pos);
}
}
else
{
// nothing but whitespace, erase it all!
str.erase(str.begin(), str.end());
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote