Student Records
Problem: Write a menu-driven C++ program that offers the following menu to the user:
Read List of Student Records
Display All Student Records
Display One Student Record
Sort Student Records
Add a new Student Record
Produce Grade Report
Exit
The menu options are described as follows:
Read List of Student Records: Read from an input file “students.txt” a list of student records. The input file contains an unknown number of student records. Each line in the input file includes the following information about one student: student id, student name (first and last), and exam scores (maximum 3 scores). The student ids must be stored in an array called IDS. The student names must be stored in an array called NAMES. The exam scores must be stored in a two-dimensional array called SCORES. The following is an example of what the input file may contain:
67890/13 Faisal Al-Hinai 85.0 78.5 89.0
12345/12 Laila Al-Zadjali 67.5 76.0 82.5
98765/12 Alia Al-Rahbi 75.5 87.0 79.5
43210/14 Salim Al-Amri 88.0 92.5 93.0
Display All Student Records: Display all student records (id, name, and all exam scores).
Display One Student Record: Search for a given student id (entered by the user) and display the corresponding student record (id, name, and all exam scores) if found, otherwise student not found is displayed.
Sort Student Records: Sort the arrays IDS, NAMES, and SCORES by increasing student id.
Add a new Student Record: Add a new student record at the end of the lists.
Produce Grade Report: Produce a grade report in an output file called “grades.txt” in the format shown below. The report must be sorted by increasing student id and must include the student letter grades and average exam scores as shown in the following example:
ID NAME EXAM 1 EXAM 2 EXAM 3 AVG Grade
————————————————————————-
12345/12 Laila Al-Zadjali 67.5 76.0 82.5 75.3 C
98765/12 Alia Al-Rahbi 75.5 87.0 79.5 80.7 B
67890/13 Faisal Al-Hinai 85.0 78.5 89.0 84.2 B
43210/14 Salim Al-Amri 88.0 92.5 93.0 91.2 A
————————————————————————-
Averages: 79.0 83.5 86.0 82.9
The letter grade for a student is based on his/her average in the exam scores using the following rules:
Average of the Exam Scores Grade
average ≥ 90 A
80 ≤ average < 90 B
70 ≤ average < 80 C
60 ≤ average < 70 D
average < 60 F
Solution
#include
#include
#include
using namespace std;
#define GRADE_A 90
#define GRADE_B 80
#define GRADE_C 70
#define GRADE_D 60
#define INPUT “students.txt”
#define OUTPUT “grades.txt”
intnumOfStudent = 0;
string IDS[100];
string NAMES[100];
double SCORES[100][3];
voidprintMenu();
voidreadListOfStudent();
voiddisplayAllStudentRecords();
voiddisplayOneStudentRecords();
voidsortStudentRecords();
voidaddNewStudentRecords();
voidproduceGradeReport();
stringcalculateLetterGrade(intstudentIndex);
doublecalculateAverageOneExam(intExamIndex);
doublecalculateAverageOneStudent(intstudentIndex);
doublecalculateAverageAllExam();
voidparseOneStudent(intstudentIndex, string line);
int main(intagrc, char* agrv[])
{
int choice = 0;
cout.precision(1);
cout<< fixed;
while (choice != 7)
{
cout<
printMenu();
cout<< “Choice: “;
cin>> choice;
cin.ignore();
switch (choice)
{
case 1:
// Read from file
readListOfStudent();
break;
case 2:
// Display all student
displayAllStudentRecords();
break;
case 3:
// Display one student
displayOneStudentRecords();
break;
case 4:
// sort student
sortStudentRecords();
cout<< “Affter sort:” <
displayAllStudentRecords();
break;
case 5:
// add new student
addNewStudentRecords();
break;
case 6:
//procude grade report
produceGradeReport();
break;
case 7:
// quit
return 0;
break;
default:
cout<< “Invalid choice. Please choice again.” <
break;
}
}
return 0;
}
/*
Print the menu
*/
voidprintMenu()
{
cout<< “1. Read List of Students Records” <
cout<< “2. Display All Students Records” <
cout<< “3. Display One Students Records” <
cout<< “4. Sort Student Records” <
cout<< “5. Add a new Student Records” <
cout<< “6. ProduceGradeReport” <
cout<< “7. Exit” <
}
/*
Read from an input file “students.txt” a list of student records. The
input file contains an unknown number of student records. Each line in the input file includes the
following information about one student: student id, student name (first and last), and exam scores
(maximum 3 scores). The student ids must be stored in an array called IDS. The student names must
be stored in an array called NAMES. The exam scores must be stored in a two-dimensional array
called SCORES
*/
voidreadListOfStudent()
{
string line;
ifstreaminputFile(INPUT);
if (inputFile.is_open())
{
while (getline(inputFile, line))
{
parseOneStudent(numOfStudent, line);
numOfStudent++;
}
inputFile.close();
}
else
{
cout<< “Unable to open file: ” << INPUT <
}
}
/*Display all student records (id, name, and all exam scores).*/
voiddisplayAllStudentRecords()
{
cout<< “ID\tNAME\t\tEXAM1\tEXAM2\tEXAM3” <
for (int i = 0; i
{
cout<< IDS[i] << “\t” << NAMES[i] << “\t\t” << SCORES[i][0] << “\t” << SCORES[i][1] << “\t” << SCORES[i][2] <
}
}
/*
Search for a given student id (entered by the user) and display the
corresponding student record (id, name, and all exam scores) if found, otherwise student not found is
displayed.
*/
voiddisplayOneStudentRecords()
{
string id;
cout<< “Enter the student ID want to display: “;
getline(cin, id);
int i = 0;
int count = 0;
for (i = 0; i
{
if (IDS[i] == id)
{
cout<< “ID\tNAME\t\tEXAM1\tEXAM2\tEXAM3” <
cout<< IDS[i] << “\t” << NAMES[i] << “\t\t” << SCORES[i][0] << “\t” << SCORES[i][1] << “\t” << SCORES[i][2] <
count++;
}
}
if (count == 0)
{
cout<< “The sudent with ID ” << id << ” did NOT exist” <
}
}
/*Sort the arrays IDS, NAMES, and SCORES by increasing student id.*/
voidsortStudentRecords()
{
for (int i = 0; i
{
for (int j = i; j
{
if (IDS[i] > IDS[j])
{
//swap ID
string temp = IDS[i];
IDS[i] = IDS[j];
IDS[j] = temp;
//swap NAME
temp = NAMES[i];
NAMES[i] = NAMES[j];
NAMES[j] = temp;
//swap SCORES
double score1, score2, score3;
score1 = SCORES[i][0];
score2 = SCORES[i][1];
score3 = SCORES[i][2];
SCORES[i][0] = SCORES[j][0];
SCORES[i][1] = SCORES[j][1];
SCORES[i][2] = SCORES[j][2];
SCORES[j][0] = score1;
SCORES[j][1] = score2;
SCORES[j][2] = score3;
}
}
}
}
/*Add a new student record at the end of the lists.*/
voidaddNewStudentRecords()
{
cout<< “Enter ID of student: “;
getline(cin, IDS[numOfStudent]);
cout<< “Enter Name of student: “;
getline(cin, NAMES[numOfStudent]);
cout<< “Enter score of EXAM1: “;
string temp;
getline(cin, temp);
SCORES[numOfStudent][0] = stod(temp);
cout<< “Enter score of EXAM2: “;
temp.clear();
getline(cin, temp);
SCORES[numOfStudent][1] = stod(temp);
cout<< “Enter score of EXAM3: “;
temp.clear();
getline(cin, temp);
SCORES[numOfStudent][2] = stod(temp);
numOfStudent++;
}
/*
Produce a grade report in an output file called “grades.txt” in the format
shown below. The report must be sorted by increasing student id and must include the student letter
grades and average exam scores
*/
voidproduceGradeReport()
{
//sort first
sortStudentRecords();
ofstreamoutputFile(OUTPUT);
if (outputFile.is_open())
{
outputFile.precision(1);
outputFile<< fixed;
outputFile<< “ID\t\t\tNAME\t\tEXAM1\tEXAM2\tEXAM3\t\tAVG\tGRAGE” <
outputFile<< “——————————————————————————————-” <
for (int i = 0; i
{
outputFile<< IDS[i] << “\t” << NAMES[i] << “\t\t” << SCORES[i][0] << “\t” << SCORES[i][1] << “\t” << SCORES[i][2] << “\t\t” <
}
outputFile<< “——————————————————————————————-” <
outputFile<< “Averages:” << “\t\t\t\t\t\t” <
outputFile.close();
}
else
{
cout<< “Unable open to write file: ” << OUTPUT <
}
}
/*Parse all infor of student from line*/
voidparseOneStudent(intstudentIndex, string line)
{
intindex_holder = 0;
string student[5];
int index = 0;
for (size_t i = 0; i
{
if (line[i] == ‘\t’ || (i == line.size() – 1))
{
if (i != index_holder)
{
student[index] = line.substr(index_holder, i – index_holder); // Parse all to array string.
index_holder = i + 1;
index = index + 1;
}
else
{
index_holder = i + 1;
}
}
}
IDS[studentIndex] = student[0]; // id
NAMES[studentIndex] = student[1]; //name
SCORES[studentIndex][0] = stod(student[2]); //score 1
SCORES[studentIndex][1] = stod(student[3]); //score 2
SCORES[studentIndex][2] = stod(student[4]); //score 3
}
/*Calculate Averages of One Student*/
doublecalculateAverageOneStudent(intstudentIndex)
{
inttotalEXAM = 0;
if (SCORES[studentIndex][0] != 0)
totalEXAM++;
if (SCORES[studentIndex][1] != 0)
totalEXAM++;
if (SCORES[studentIndex][2] != 0)
totalEXAM++;
return (SCORES[studentIndex][0] + SCORES[studentIndex][1] + SCORES[studentIndex][2]) / totalEXAM;
}
/*Calculate Letter Grade of Student*/
stringcalculateLetterGrade(intstudentIndex)
{
double averages = calculateAverageOneStudent(studentIndex);
if (averages >= 90)
return “A”;
else if (averages >= 80)
return “B”;
else if (averages >= 70)
return “C”;
else if (averages >= 60)
return “D”;
return “F”;
}
/*Calculate Average One Exam*/
doublecalculateAverageOneExam(intexamIndex)
{
intcountExam = 0;
double total = 0;
for (int i = 0; i
{
if (SCORES[i][examIndex] != 0)
{
countExam++;
total += SCORES[i][examIndex];
}
}
return total / countExam;
}
/*Calculate Average All Exam*/
doublecalculateAverageAllExam()
{
int count = 0;
double total = 0;
for (int i = 0; i < 3; ++i)
{
double temp = calculateAverageOneExam(i);
if (temp != 0)
{
count++;
total += temp;
}
}
return total / count;
}