C++
// Write a program that reads student names from a file followed by their test scores. // The program should then output each students name followed by the test score and the relevant grade. // It should also print the highest test score and the name of students having the highest test scores. // //Specifically, write a code to : // Read from the file ("inData.txt") and store in a struct variable of type studentType, // which has four components : studentFName, and studentLName of type string, // testScore of type int(between the range of 0 and 100), and grade of type char. // // Assume the class has 20 students. Use any array of 20 components of type studenetType. // // Your program should contain the definition of the following functions : // // function to read the students' data into the array // function to assign the relevant grade to each student // function to find the highest test score // function to print the name of the students having the highest test score. // #include <iostream> #include <fstream> #include <string> #include <algorithm> #include <iomanip> #include <algorithm> using namespace std; const int STUDENTS = 20; /////////////////////////////////////////////////////////////////////////////// struct Student { string sStudentFName; string sStudentLName; int iTestScore; char cGrade; }; /////////////////////////////////////////////////////////////////////////////// bool ReadStudentData(string sFileName, Student students[], const int iSize) { ifstream iFile(sFileName); if (!iFile) { cout << "Please specify a file name." << endl; system("PAUSE"); return 1; } int iCount = 0; while (!iFile.eof() && iCount < STUDENTS) { iFile >> students[iCount].sStudentFName; iFile >> students[iCount].sStudentLName; iFile >> students[iCount].iTestScore; iCount++; } iFile.close(); return true; } /////////////////////////////////////////////////////////////////////////////// void AssigneGrade(Student students[], const int iSize) { for (int i = 0; i < iSize; i++) { if (students[i].iTestScore > 89) { students[i].cGrade = 'A'; } else if (students[i].iTestScore > 79) { students[i].cGrade = 'B'; } else if (students[i].iTestScore > 69) { students[i].cGrade = 'C'; } else if (students[i].iTestScore > 59) { students[i].cGrade = 'D'; } else { students[i].cGrade = 'E'; } } } /////////////////////////////////////////////////////////////////////////////// Student GetStudentHighestScore(Student students[], const int iSize) { int iHighestIndex = 0; int iHighestScore = 0; for (int i = 0; i < iSize; i++) { if (students[i].iTestScore > iHighestScore) { iHighestScore = students[i].iTestScore; iHighestIndex = i; } } return students[iHighestIndex]; } /////////////////////////////////////////////////////////////////////////////// void PrintHeading(string sTitle) { /// Print header.... cout << endl << endl; cout << sTitle << endl; cout << left << setfill(' ') << setw(16); cout << "Name:"; cout << left << setfill(' ') << setw(9); cout << "Score:"; cout << "Grade:" << endl; cout << left << setfill('-') << setw(30) << '-' << endl; } /////////////////////////////////////////////////////////////////////////////// void PrintStudentInfo(Student students[], const int iSize) { /// Get the longest student name (first and last). static size_t iLongestFullName = 0; for (int i = 0; i < iSize; i++) { string sFullName = students[i].sStudentFName + ", " + students[i].sStudentLName; if (iLongestFullName < sFullName.length()) iLongestFullName = sFullName.length(); } /// Print the scores.... for (int i = 0; i < iSize; i++) { string sFullName = students[i].sStudentFName + ", " + students[i].sStudentLName; cout << left << setfill('.') << setw(16); cout << sFullName; cout << students[i].iTestScore; cout << right << setfill('.') << setw(8); cout << students[i].cGrade; cout << endl; } } /////////////////////////////////////////////////////////////////////////////// void PrintStudentInfoRange(const int iMinScore, const int iMaxScore, Student students[], const int iSize) { // Happy code goes here.... } /////////////////////////////////////////////////////////////////////////////// const float GetAverageScore(Student students[], const int iSize) { // Happy code goes here.... } /////////////////////////////////////////////////////////////////////////////// int main(int argc, char *argv[]) { if (argc < 2) { cout << "Please specify a file name." << endl; system("PAUSE"); return 1; } ////// Student Students[STUDENTS] = {}; if (ReadStudentData(argv[1], Students, STUDENTS) == false) { cout << "Error with ReadStudentData." << endl; system("PAUSE"); return 1; } AssigneGrade(Students, STUDENTS); Student StudentHighestScore = GetStudentHighestScore(Students, STUDENTS); PrintHeading("All Students"); PrintStudentInfo(Students, STUDENTS); PrintHeading("High Score Student"); PrintStudentInfo(&StudentHighestScore, 1); PrintHeading("Range Test \"A's\""); PrintStudentInfoRange(90, 100, Students, STUDENTS); PrintHeading("Range Test \"B's\""); PrintStudentInfoRange(80, 89, Students, STUDENTS); PrintHeading("Range Test \"C's\""); PrintStudentInfoRange(70, 79, Students, STUDENTS); PrintHeading("Range Test \"D's\""); PrintStudentInfoRange(60, 69, Students, STUDENTS); PrintHeading("Range Test \"E's\""); PrintStudentInfoRange(0, 59, Students, STUDENTS); cout << endl << "The average score is " << fixed << setprecision(2) << GetAverageScore(Students, STUDENTS) << endl; system("PAUSE"); return 0; }