In: Computer Science
Please do not reply to this question by copy pasting the code that someone wrote for this question because it is incorrect. The code written before for this messes up two vectors. Please make sure you write a new code without that error.
8.10 Project 4 - Help me Sort My Roster
About
The class roster program is working really well. However, it would be nice to be able to sort students based on first name, last name, or their grade. I would like a program where I can choose how my roster is sorted and then print out the class summary
Specification
Unlike past assignments which test matching output this assignment tests output along with testing your functions directly using unit tests. The unit tests will use your functions with hardcoded vectors, if the tests fails the unit tests will print out what it expected compared to what your function did when sorting. If you have any questions please reach out via email, discussion board, or during office hours.
#include <iostream>
#include <vector>
#include <string>
#include <locale>
#include <iomanip>
using namespace std;
void PrintWelcome();
void GetNumberOfStudents(int &numOfStudents);
void PrintMenu();
char GetUserSelection();
void PrintSummary(const vector<string> &students, const
vector<double> &grades);
void AddStudent(vector<string> &students,
vector<double> &grades);
void RemoveStudent(vector<string> &students,
vector<double> &grades);
void SortByFirstName(vector<string> &students,
vector<double> &grades);
void SortByLastName(vector<string> &students,
vector<double> &grades);
void SortByGrade(vector<string> &students,
vector<double> &grades);
int main() {
const char QUIT = 'q';
const char ADD = 'a';
const char REMOVE = 'r';
const char PRINT = 'p';
const char MENU = 'm';
string thankYou = "Thank you for entering your students
information!\n";
string notValid = "Not a valid selection.\n";
char selection;
int numOfStudents;
vector<string> students;
vector<double> grades;
//Print the Welcome Message
PrintWelcome();
//Get Number of Students
GetNumberOfStudents(numOfStudents);
//Add the total number of students to the student and grades
vectors
for(int i = 0; i <numOfStudents; i++){
AddStudent(students, grades);
}
//Print thank you message
cout << thankYou;
//Print the Roster Menu
PrintMenu();
//Get the users selection
selection = GetUserSelection();
while(selection != QUIT){
if(selection == ADD){
AddStudent(students, grades);
}
else if(selection == REMOVE){
RemoveStudent(students, grades);
}
else if(selection == PRINT){
PrintSummary(students, grades);
}
else if(selection == MENU){
PrintMenu();
}
/*Provide Implementation for other menu options*/
else{
cout << notValid;
}
selection = GetUserSelection();
}
}
void PrintWelcome(){
string welcome = "Welcome to the student roster!\n";
cout << welcome;
}
void GetNumberOfStudents(int &numOfStudents){
string numOfStudentsQuestion = "How many students are in your
class?:\n";
cout << numOfStudentsQuestion;
cin >> numOfStudents;
}
void PrintMenu(){
string menu = "Please choose one of the following options:\n"
"a: add a student\n"
"r: remove a student\n"
"p: print the class summary\n"
"m: print menu\n"
"f: sort - first name\n"
"l: sort - last name\n"
"g: sort - grade\n"
"q: quit program\n";
cout << menu;
}
char GetUserSelection(){
string selectionString = "selection:\n";
char selection;
cout << selectionString;
cin >> selection;
return selection;
}
void PrintSummary(const vector<string> &students, const
vector<double> &grades){
string summaryHeading = "Class Summary\n"
"------------------------\n";
string nameGradeHeading = "Name Grade\n"
"--------- --------\n";
string numOfStudentsString = "Number of Students:\n"
"-------------------\n";
string averageGradeString = "Average Grade:\n"
"--------------\n";
double sum = 0.0;
double average = 0.0;
int numOfStudents = students.size();
cout << endl;
cout << summaryHeading << nameGradeHeading;
for(int i = 0; i < students.size(); i++){
sum += grades.at(i);
cout << left << setw(20) << students.at(i)
<< setprecision(2) << fixed << grades.at(i)
<< endl;
}
cout << numOfStudentsString << numOfStudents <<
endl;
cout << averageGradeString << setprecision(2) <<
fixed << sum/numOfStudents << endl;
cout << endl;
}
void AddStudent(vector<string> &students,
vector<double> &grades){
string studentInfo = "Please enter student (First Last Grade)
info:\n";
string firstName, lastName;
double grade;
cout << studentInfo;
cin >> firstName >> lastName >> grade;
students.push_back(firstName + " " + lastName);
grades.push_back(grade);
}
void RemoveStudent(vector<string> &students,
vector<double> &grades){
string removeStudent = "Please enter students first and last
name";
string firstName, lastName;
cout << removeStudent;
cin >> firstName >> lastName;
string fullName = firstName + " " + lastName;
for(int i = 0; i < students.size(); i++){
if(students.at(i) == fullName) {
students.erase(students.begin() + i);
grades.erase(grades.begin() + i);
cout << "Removing: " << fullName;
}
}
}
void SortByFirstName(vector<string> &students,
vector<double> &grades){
/*Provide Implementation*/
}
void SortByLastName(vector<string> &students,
vector<double> &grades){
/*Provide Implementation*/
}
void SortByGrade(vector<string> &students,
vector<double> &grades){
/*Provide Implementation*/
}
Please find below the code, code screenshots and output screenshots. Please refer to the screenshot of the code to understand the indentation of the code. Please get back to me if you need any change in code. Else please upvote
CODE:
#include <iostream>
#include <vector>
#include <string>
#include <locale>
#include <iomanip>
using namespace std;
void PrintWelcome();
void GetNumberOfStudents(int &numOfStudents);
void PrintMenu();
char GetUserSelection();
void PrintSummary(const vector<string> &students, const vector<double> &grades);
void AddStudent(vector<string> &students, vector<double> &grades);
void RemoveStudent(vector<string> &students, vector<double> &grades);
void SortByFirstName(vector<string> &students, vector<double> &grades);
void SortByLastName(vector<string> &students, vector<double> &grades);
void SortByGrade(vector<string> &students, vector<double> &grades);
int main() {
const char QUIT = 'q';
const char ADD = 'a';
const char REMOVE = 'r';
const char PRINT = 'p';
const char MENU = 'm';
const char SORT_FNAME = 'f';
const char SORT_LNAME = 'l';
const char SORT_GRADE = 'g';
string thankYou = "Thank you for entering your students information!\n";
string notValid = "Not a valid selection.\n";
char selection;
int numOfStudents;
vector<string> students;
vector<double> grades;
//Print the Welcome Message
PrintWelcome();
//Get Number of Students
GetNumberOfStudents(numOfStudents);
//Add the total number of students to the student and grades vectors
for(int i = 0; i <numOfStudents; i++){
AddStudent(students, grades);
}
//Print thank you message
cout << thankYou;
//Print the Roster Menu
PrintMenu();
//Get the users selection
selection = GetUserSelection();
while(selection != QUIT){
if(selection == ADD){
AddStudent(students, grades);
}
else if(selection == REMOVE){
RemoveStudent(students, grades);
}
else if(selection == PRINT){
PrintSummary(students, grades);
}
else if(selection == MENU){
PrintMenu();
}
else if(selection == SORT_FNAME){
SortByFirstName(students, grades);
}
else if(selection == SORT_LNAME){
SortByLastName(students, grades);
}
else if(selection == SORT_GRADE){
SortByGrade(students, grades);
}
else{
cout << notValid;
}
selection = GetUserSelection();
}
}
void PrintWelcome(){
string welcome = "Welcome to the student roster!\n";
cout << welcome;
}
void GetNumberOfStudents(int &numOfStudents){
string numOfStudentsQuestion = "How many students are in your class?:\n";
cout << numOfStudentsQuestion;
cin >> numOfStudents;
}
void PrintMenu(){
string menu = "Please choose one of the following options:\n"
"a: add a student\n"
"r: remove a student\n"
"p: print the class summary\n"
"m: print menu\n"
"f: sort - first name\n"
"l: sort - last name\n"
"g: sort - grade\n"
"q: quit program\n";
cout << menu;
}
char GetUserSelection(){
string selectionString = "selection:\n";
char selection;
cout << selectionString;
cin >> selection;
return selection;
}
void PrintSummary(const vector<string> &students, const vector<double> &grades){
string summaryHeading = "Class Summary\n"
"------------------------\n";
string nameGradeHeading = "Name Grade\n"
"--------- --------\n";
string numOfStudentsString = "Number of Students:\n"
"-------------------\n";
string averageGradeString = "Average Grade:\n"
"--------------\n";
double sum = 0.0;
double average = 0.0;
int numOfStudents = students.size();
cout << endl;
cout << summaryHeading << nameGradeHeading;
for(int i = 0; i < students.size(); i++){
sum += grades.at(i);
cout << left << setw(20) << students.at(i) << setprecision(2) << fixed << grades.at(i) << endl;
}
cout << numOfStudentsString << numOfStudents << endl;
cout << averageGradeString << setprecision(2) << fixed << sum/numOfStudents << endl;
cout << endl;
}
void AddStudent(vector<string> &students, vector<double> &grades){
string studentInfo = "Please enter student (First Last Grade) info:\n";
string firstName, lastName;
double grade;
cout << studentInfo;
cin >> firstName >> lastName >> grade;
students.push_back(firstName + " " + lastName);
grades.push_back(grade);
}
void RemoveStudent(vector<string> &students, vector<double> &grades){
string removeStudent = "Please enter students first and last name";
string firstName, lastName;
cout << removeStudent;
cin >> firstName >> lastName;
string fullName = firstName + " " + lastName;
for(int i = 0; i < students.size(); i++){
if(students.at(i) == fullName) {
students.erase(students.begin() + i);
grades.erase(grades.begin() + i);
cout << "Removing: " << fullName;
}
}
}
void SortByFirstName(vector<string> &students, vector<double> &grades){
int vecsize = students.size();
for (int j = 0; j < vecsize - 1; ++j) {
int min = j;
for (int i = j+1; i < vecsize; ++i) {
if (students.at(min) > students.at(i)) {
min = i;
}
}
if (min != j){
string temp = students.at(j);
students.at(j) = students.at(min);
students.at(min) = temp;
double temp1 = grades.at(j);
grades.at(j) = grades.at(min);
grades.at(min) = temp1;
}
}
cout <<"Student data sorted by first name.\n";
}
void SortByLastName(vector<string> &students, vector<double> &grades){
int vecsize = students.size();
for (int j = 0; j < vecsize - 1; ++j) {
int min = j;
for (int i = j+1; i < vecsize; ++i) {
string min_lname = students.at(min).substr(students.at(min).find(' ')+1, students.at(min).length());
string i_lname = students.at(i).substr(students.at(i).find(' ')+1, students.at(min).length());
if (min_lname > i_lname) {
min = i;
}
}
if (min != j){
string temp = students.at(j);
students.at(j) = students.at(min);
students.at(min) = temp;
double temp1 = grades.at(j);
grades.at(j) = grades.at(min);
grades.at(min) = temp1;
}
}
cout <<"Student data sorted by last name.\n";
}
void SortByGrade(vector<string> &students, vector<double> &grades){
int vecsize = grades.size();
for (int j = 0; j < vecsize - 1; ++j) {
int min = j;
for (int i = j+1; i < vecsize; ++i) {
if (grades.at(min) > grades.at(i)) {
min = i;
}
}
if (min != j){
double temp1 = grades.at(j);
grades.at(j) = grades.at(min);
grades.at(min) = temp1;
string temp = students.at(j);
students.at(j) = students.at(min);
students.at(min) = temp;
}
}
cout <<"Student data sorted by grades.\n";
}
OUTPUT: