In: Computer Science
in c++ you need to write a program that maintains the gradebook
for a specific course. For each student registered in this course,
the program keeps track of the student’s name, identification
number (id), four exam grades, and final grade. The program
performs several functionalities such as: enrolling a student in
the course, dropping a student from the course, uploading student’s
exam grades, displaying the grades of a specific student,
displaying the grades of all students in the course, and printing
grades summary information.
To do that, you need to create a structure, Student, that holds
individual student’s details. The Student struct contains the
following fields:
struct Student { string name; // student first and last name long
id; // student identification number double grades[4]; // array
holding four exam grades double finalGrade; // student final grade
}
Then, in your main program, you need to:
- declare a vector of Student structs, named students, to hold the
data for all the students enrolled in this course. - process a
transaction file, “StudentsTrans.txt”. Each line in the file
contains a specific command such as “enroll”, “display”, “drop”,
“uploadGrades”, “searchByName”, and “printClassSummary”. The line
also contains all necessary data to process the command. Each
command must be implemented in a separate function. The commands
are:
▪ Enroll StudentFirstName StudentLastName StudentID
This command adds a new Student record (object) to the students’
vector. You should read the student details from the transaction
file (StudentFirstName, StudentLastName, StudentID). Before
creating a new object, you need to check for duplicate student
entries (if an existing student has the same StudentID). If this
student was not previously added to the students vector, you should
add an entry for that student and its details at the end of the
students’ vector. Make sure to concatenate StudentFirstName and
StudentLastName into one student name and set all grades initially
to zeros. You can simply use the push_back function to add a
Student object to the end of the vector. You should also display a
message that the student was successfully added to the vector. For
example:
enroll Joe Garcia 432345673 Joe Garcia successfully added to
students vector…
If this student already exists in the vector, you should not add
the student again to the vector. You should also display an error
message. For example:
enroll Amelia Perez 876007654 Student already enrolled in this
class, can not enroll again
▪ Display
This command displays the details of all students registered in the
course in a neat table format. Details include a student’s name,
id, four individual exam grades, and the final grade. The
final
grade is the average of the four exam scores. You should print
decimal numbers with 2 digits of precision. A sample display is
shown below:
Student ID Exam1 Exam2 Exam3 Exam4 Final
Grade
-------------------------------------------------------------------------
Suzy Walter 876523067 87.50 85.00 88.00 81.00 85.38
Amelia Perez 876007654 62.00 70.00 71.50
80.00 70.88 …
▪ SearchByName Keyword
This command searches and prints the records of all students whose
first names, last names, or full names include or match Keyword.
For example, if Keyword is “James”, you should display the
following students:
searchByName James Student ID Exam1 Exam2 Exam3 Exam4 FinalGrade
------------------------------------------------------------------------
James Ducrest 123282345 62.00 67.00 63.00 70.00 66.25 James Didier
123456789 79.00 85.00 80.00 88.00 83.00
If no matching entry is found, you should display a message that
this student is not enrolled in this course. For example:
searchByName Michel No such student exists
▪ Drop StudentID
This command drops a specific student (whose id is StudentID) from
the course by removing its entry from the students’ vector. You
should check if that student is enrolled in this class before
trying to delete its entry. If so, pay attention that deleting
could be from any entry in the vector and not necessarily the last
record. You should also display a message that the student was
successfully deleted from the vector. For example:
drop 982323146… Andrea Meyer successfully dropped from course
If no matching entry is found, you should display a message that
this student does not exist. For example:
drop 012345634… No such student exists
▪ UploadGrades StudentID Grade1 Grade2 Grade3 Grade4
This command uploads the grades of a specific student to the
corresponding entry in the students vector. The command specifies
StudentID, which corresponds to the id of the student whose grades
should be uploaded. The command also specifies the 4 individual
exam grades for that student (Grade1 Grade2 Grade3 Grade4). You
should search for the student whose id matches StudentID. If a
match is found, you should update its grades array and display a
message that the student’s grades were successfully uploaded. For
example:
uploadGrades 876523067 87.5 85 88 81 Suzy walter grades
successfully uploaded
Otherwise, you should display a message that this student was not
found. For example:
uploadGrades 999999999 60 90 70 80 no such student exists, grades
can not be uploaded
▪ PrintClassSummary
This command prints summary information. This includes: the name of
the student who took the highest final grade in this course, the
name of the student who took the lowest final grade in this course,
and the overall class average.
To test your code, you may create and process the following
transaction file:
“StudentsTrans.txt”
enroll Joe Garcia 432345673
enroll Suzy Walter 876523067
enroll Amelia Perez 876007654
enroll James Ducrest 123282345
enroll Amelia Perez 876007654
enroll Suzy Walter 987667654
enroll James Didier 123456789
enroll Carolyn Johnson 456283409
enroll Andrea Meyer 982323146
enroll Edward Weber 787878123
display
drop 012345634
drop 982323146
drop 432345673
uploadGrades 432345673 98 98 99 93
uploadGrades 999999999 60 90 70 80
uploadGrades 876523067 87.5 85 88 81
uploadGrades 876007654 62 70 71.5 80
uploadGrades 123282345 62 70 63 70
uploadGrades 987667654 81 83 75 90
uploadGrades 123456789 79 85 80 88
uploadGrades 456283409 95 91 92 94
uploadGrades 787878123 99 98.5 95 94
searchByName James
searchByName John
searchByName Edward
searchByName Michel
searchByName Amelia Perez
display printClassSummary
Screenshot
------------------------------------------------------------------------------------
Program
//Header files
#include <iostream>
#include<string>
#include<vector>
#include<fstream>
#include<iomanip>
using namespace std;
//Create a student struct
struct Student {
string name;
long id;
double grades[4];
double finalGrade;
};
//function prototypes
void enroll(vector<Student>& course,string line);
void display(vector<Student> course);
void drop(vector<Student>& course, string line);
void upload(vector<Student>& course,string line);
void search(vector<Student>& course,string line);
void summary(vector<Student> course);
int main()
{
//Vector for student's of a course
vector<Student> course;
//file open
ifstream in("StudentsTrans.txt");
//Error check
if (!in) {
cout << "File not found!!!"
<< endl;
exit(0);
}
string line;
while (getline(in,line)) {
string cmd=
line.substr(0,line.find(' '));
if (cmd == "enroll") {
enroll(course,
line);
}
else if (cmd == "display"
&& cmd.length()==line.length()) {
display(course);
}
else if (cmd == "drop") {
drop(course,line);
}
else if (cmd == "uploadGrades")
{
upload(course,
line);
}
else if (cmd == "searchByName")
{
search(course,
line);
}
else if (cmd == "display") {
summary(course);
}
cout << endl;
}
}
//Function to read a student details and enrol him in the
course
//Before enrolling check if already that student present or
not
void enroll(vector<Student>& course,string line) {
string delimiter = " ";
string cmd = line.substr(0,
line.find(delimiter));
line.erase(0, line.find(delimiter) +
delimiter.length());
string fName= line.substr(0,
line.find(delimiter));
line.erase(0, line.find(delimiter) +
delimiter.length());
string lName = line.substr(0,
line.find(delimiter));
line.erase(0, line.find(delimiter) +
delimiter.length());
long id =stol(line);
for (int i = 0; i < course.size(); i++) {
if (course[i].id == id) {
cout <<
cmd<<" " << fName << " " << lName <<
" " << id << " Student already enrolled in this class,
can not enroll again" << endl;
return;
}
}
Student s;
s.name = fName + " " + lName;
s.id = id;
s.grades[0] = 0;
s.grades[1] = 0;
s.grades[2] = 0;
s.grades[3] = 0;
s.finalGrade = (s.grades[0]+ s.grades[1]+ s.grades[2]+
s.grades[3])/4;
course.push_back(s);
cout <<cmd<< " " << fName << "
" << lName << " " << id <<"
"<<s.name<<" successfully added to students vector
…"<< endl;
}
//Function to display students details
void display(vector<Student> course) {
cout << fixed<<setprecision(2);
cout << "
Student
ID
Exam1
Exam2
Exam3
Exam4 Final
Grade\n";
cout <<
"------------------------------------------------------------------------------------------------------------------------\n";
for (int i = 0; i < course.size(); i++) {
cout << setw(17) <<
course[i].name << setw(15) << course[i].id
<<"\t\t" << course[i].grades[0]
<< "\t\t"
<< course[i].grades[1] << "\t\t" <<
course[i].grades[2] << "\t\t"
<<
course[i].grades[3] << "\t\t" << course[i].finalGrade
<< endl;
}
}
//Function to drop a student from the course if matches the
id
void drop(vector<Student>& course,string line) {
string delimiter = " ";
string cmd = line.substr(0,
line.find(delimiter));
line.erase(0, line.find(delimiter) +
delimiter.length());
long id = stol(line);
for (int i = 0; i < course.size(); i++) {
if (course[i].id == id) {
cout
<<cmd<<" " << id << "... " <<
course[i].name << " successfully dropped from course"
<< endl;
course.erase((course.begin() + i));
return;
}
}
cout << cmd << " " << id << "…
No such student exists" << endl;
}
//Functio read data from file and match with id to updates grade
details of a student
void upload(vector<Student>& course,string line) {
string delimiter = " ";
string cmd = line.substr(0,
line.find(delimiter));
line.erase(0, line.find(delimiter) +
delimiter.length());
long id = stol(line.substr(0,
line.find(delimiter)));
line.erase(0, line.find(delimiter) +
delimiter.length());
double m1 =stod( line.substr(0,
line.find(delimiter)));
line.erase(0, line.find(delimiter) +
delimiter.length());
double m2 = stod(line.substr(0,
line.find(delimiter)));
line.erase(0, line.find(delimiter) +
delimiter.length());
double m3 = stod(line.substr(0,
line.find(delimiter)));
line.erase(0, line.find(delimiter) +
delimiter.length());
double m4 = stod(line);
cout << fixed << setprecision(2);
for (int i = 0; i < course.size(); i++) {
if (course[i].id == id) {
course[i].grades[0] = m1;
course[i].grades[1] = m2;
course[i].grades[2] = m3;
course[i].grades[3] = m4;
course[i].finalGrade= (m1+m2+m3+m4)/4;
cout <<
"uploadGrades " << id << " " << m1 << " "
<< m2 << " " << m3 << " " << m4
<< " "
<< course[i].name << " grades
successfully uploaded" << endl;
return;
}
}
cout << "uploadGrades " << id << " "
<< m1 << " " << m2 << " " << m3
<< " " << m4 << " "
<<"no such student exists,
grades can not be uploaded"<< endl;
}
//Function to search a student by his name
void search(vector<Student>& course,string line) {
string delimiter = " ";
string cmd = line.substr(0,
line.find(delimiter));
line.erase(0, line.find(delimiter) +
delimiter.length());
string name = line;
int j = 0;
bool check = false;
cout << "searchByName " << name <<
endl;
for (int i = 0; i < course.size(); i++) {
if (course[i].name.find(name) !=
std::string::npos) {
check =
true;
if (j == 0)
{
cout << "
Student
ID
Exam1
Exam2
Exam3
Exam4 Final
Grade\n";
cout <<
"------------------------------------------------------------------------------------------------------------------------\n";
cout << setw(17) << course[i].name
<< setw(15) << course[i].id << "\t\t" <<
course[i].grades[0]
<< "\t\t" <<
course[i].grades[1] << "\t\t" << course[i].grades[2]
<< "\t\t"
<< course[i].grades[3]
<< "\t\t" << course[i].finalGrade << endl;
j++;
}
else {
cout << setw(17) << course[i].name
<< setw(15) << course[i].id << "\t\t" <<
course[i].grades[0]
<< "\t\t" <<
course[i].grades[1] << "\t\t" << course[i].grades[2]
<< "\t\t"
<< course[i].grades[3]
<< "\t\t" << course[i].finalGrade << endl;
}
}
}
if (!check) {
cout << "No such student
exists" << endl;
}
}
//Function to generate summary
void summary(vector<Student> course) {
cout << fixed << setprecision(2);
cout << "Summary of the class:-" <<
endl;
Student highest = course[0], lowest = course[0];
double avg = 0;
for (int i = 0; i < course.size(); i++) {
avg += course[i].finalGrade;
if (highest.finalGrade <
course[i].finalGrade) {
highest =
course[i];
}
if (lowest.finalGrade >
course[i].finalGrade) {
lowest =
course[i];
}
}
cout << "Highest scored student in the course:-"
<< endl;
cout << "
Student
ID
Exam1
Exam2
Exam3
Exam4 Final
Grade\n";
cout <<
"------------------------------------------------------------------------------------------------------------------------\n";
cout << setw(17) << highest.name <<
setw(15) << highest.id << "\t\t" <<
highest.grades[0]
<< "\t\t" <<
highest.grades[1] << "\t\t" << highest.grades[2]
<< "\t\t"
<< highest.grades[3] <<
"\t\t" << highest.finalGrade << endl;
cout << "\nLowest scored student in the
course:-" << endl;
cout << "
Student
ID
Exam1
Exam2
Exam3
Exam4 Final
Grade\n";
cout <<
"------------------------------------------------------------------------------------------------------------------------\n";
cout << setw(17) << lowest.name <<
setw(15) << lowest.id << "\t\t" <<
lowest.grades[0]
<< "\t\t" <<
lowest.grades[1] << "\t\t" << lowest.grades[2] <<
"\t\t"
<< lowest.grades[3] <<
"\t\t" << lowest.finalGrade << endl;
cout << "\nAverage of the class = " <<
(avg / course.size()) << endl;
}
--------------------------------------------------------------------------------
output
enroll Joe Garcia 432345673 Joe Garcia successfully added to students vector à
enroll Suzy Walter 876523067 Suzy Walter successfully added to students vector à
enroll Amelia Perez 876007654 Amelia Perez successfully added to students vector à
enroll James Ducrest 123282345 James Ducrest successfully added to students vector à
enroll Amelia Perez 876007654 Student already enrolled in this class, can not enroll again
enroll Suzy Walter 987667654 Suzy Walter successfully added to students vector à
enroll James Didier 123456789 James Didier successfully added to students vector à
enroll Carolyn Johnson 456283409 Carolyn Johnson successfully added to students vector à
enroll Andrea Meyer 982323146 Andrea Meyer successfully added to students vector à
enroll Edward Weber 787878123 Edward Weber successfully added to students vector à
Student
ID
Exam1
Exam2
Exam3
Exam4 Final Grade
------------------------------------------------------------------------------------------------------------------------
Joe
Garcia
432345673
0.00
0.00
0.00
0.00
0.00
Suzy
Walter
876523067
0.00
0.00
0.00
0.00
0.00
Amelia Perez
876007654
0.00
0.00
0.00
0.00
0.00
James Ducrest
123282345
0.00
0.00
0.00
0.00
0.00
Suzy
Walter
987667654
0.00
0.00
0.00
0.00
0.00
James Didier
123456789
0.00
0.00
0.00
0.00
0.00
Carolyn Johnson
456283409
0.00
0.00
0.00
0.00
0.00
Andrea Meyer
982323146
0.00
0.00
0.00
0.00
0.00
Edward Weber
787878123
0.00
0.00
0.00
0.00
0.00
drop 12345634à No such student exists
drop 982323146... Andrea Meyer successfully dropped from course
drop 432345673... Joe Garcia successfully dropped from course
uploadGrades 432345673 98.00 98.00 99.00 93.00 no such student exists, grades can not be uploaded
uploadGrades 999999999 60.00 90.00 70.00 80.00 no such student exists, grades can not be uploaded
uploadGrades 876523067 87.50 85.00 88.00 81.00 Suzy Walter grades successfully uploaded
uploadGrades 876007654 62.00 70.00 71.50 80.00 Amelia Perez grades successfully uploaded
uploadGrades 123282345 62.00 70.00 63.00 70.00 James Ducrest grades successfully uploaded
uploadGrades 987667654 81.00 83.00 75.00 90.00 Suzy Walter grades successfully uploaded
uploadGrades 123456789 79.00 85.00 80.00 88.00 James Didier grades successfully uploaded
uploadGrades 456283409 95.00 91.00 92.00 94.00 Carolyn Johnson grades successfully uploaded
uploadGrades 787878123 99.00 98.50 95.00 94.00 Edward Weber grades successfully uploaded
searchByName James
Student
ID
Exam1
Exam2
Exam3
Exam4 Final Grade
------------------------------------------------------------------------------------------------------------------------
James Ducrest
123282345
62.00
70.00
63.00
70.00
66.25
James Didier
123456789
79.00
85.00
80.00
88.00
83.00
searchByName John
Student
ID
Exam1
Exam2
Exam3
Exam4 Final Grade
------------------------------------------------------------------------------------------------------------------------
Carolyn Johnson
456283409
95.00
91.00
92.00
94.00
93.00
searchByName Edward
Student
ID
Exam1
Exam2
Exam3
Exam4 Final Grade
------------------------------------------------------------------------------------------------------------------------
Edward Weber
787878123
99.00
98.50
95.00
94.00
96.63
searchByName Michel
No such student exists
searchByName Amelia Perez
Student
ID
Exam1
Exam2
Exam3
Exam4 Final Grade
------------------------------------------------------------------------------------------------------------------------
Amelia Perez
876007654
62.00
70.00
71.50
80.00
70.88
Summary of the class:-
Highest scored student in the course:-
Student
ID
Exam1
Exam2
Exam3
Exam4 Final Grade
------------------------------------------------------------------------------------------------------------------------
Edward Weber
787878123
99.00
98.50
95.00
94.00
96.63
Lowest scored student in the course:-
Student
ID
Exam1
Exam2
Exam3
Exam4 Final Grade
------------------------------------------------------------------------------------------------------------------------
James Ducrest
123282345
62.00
70.00
63.00
70.00
66.25
Average of the class = 82.48