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

I created a class that stores 3 objects. ID (int), Name (string) and Grade (doub

ID: 3673839 • Letter: I

Question

I created a class that stores 3 objects. ID (int), Name (string) and Grade (double). I stored 5 initial objects and printed them at the beginning of my code. This is my code that works upto this point:

#include <iostrea>
#include <string>
#include <vector>

using namespace std;

//GradeBook class definition
class GradeBook
{
public:
   //Declare a constructor that has one parameter for each data member
   GradeBook(int, string, double);
   //Declare a set method for Student ID
   void setStudentID(int);
   //Declare a get method for Student ID
   int getStudentID();
   //Declare a set method for Student's name
   void setStudentName(string);
   //Declare a get method for Student's name
   string getStudentName();
   //Declare a set method for grade
   void setStudentGrade(double);
   //Declare a get method for grade
   double getStudentGrade();
   void displayGradeBook();
private:
   //Declare a int data member for Student ID
   int studentID;
   //Declare a string data member for student name
   string studentName;
   //Declare a double data member for grade
   double studentGrade;
}; //End Class GradeBook

// constructor initializes StudentID, StudentName and Grade
GradeBook::GradeBook(int identification, string name, double grade)
{
   setStudentID(identification);        //initializes ID Number
   setStudentName(name);                //initializes Student name
   setStudentGrade(grade);                   //initializes grade
}//end GradeBook constructor

//function to set student ID
void GradeBook::setStudentID(int identification)
{
   studentID = identification;        //store the ID
}//end function setStudentID

//function to retrieve the student ID
int GradeBook::getStudentID()
{
   return studentID;
}//end function getStudentID

//function to set student name
void GradeBook::setStudentName(string name)
{
   studentName = name;                //store the name
}//end function setStudentName

//function to retrieve the student name
string GradeBook::getStudentName()
{
   return studentName;
}//end function getStudentName

//function to set student grade
void GradeBook::setStudentGrade(double grade)
{
   studentGrade = grade;                //store the grade
}//end function setStudentGrade

//function to retrieve the student grade
double GradeBook::getStudentGrade()
{
   return studentGrade;
}//end function getStudentGrade

void GradeBook::displayGradeBook()
{
   //Display existing ID,name,and grade information already stored
   cout << getStudentID() << " " << getStudentName() << " " << getStudentGrade() << endl;
}//end function displayGradeBook

int main()
{
   cout << " Student ID Student Name Grade" << endl;
   // create a GradeBook object; pass a course name and instructor name
   GradeBook gradeBook(3, "Jim", 97.3);
   gradeBook.displayGradeBook();

   GradeBook gradeBook2(5, "Bob", 88.7);
   gradeBook2.displayGradeBook();

   GradeBook gradeBook3(2, "Tom", 77.5);
   gradeBook3.displayGradeBook();

   GradeBook gradeBook4(1, "Joe", 66.9);
   gradeBook4.displayGradeBook();
  
   GradeBook gradeBook5(4, "Lee", 83.2);
   gradeBook5.displayGradeBook();

   string command;
   cout << "5 Available Commands Are:" << endl;
   cout << "Add, Remove, Sort, Print, Quit" << endl;
   cout << "Please Enter Your Command: ";
   cin >> command;

return 0; // indicate successful termination
}//end main

I am stuck at this point. I need to ask the user to Add, Remove, Print, Sort. I dont know how to take the user input and manipulate objects in the class. If a user enters "sort", I don't know how to sort the ID numbers or sort the grades. Also, the assignment gives a hint that we have to use iterator to find the ID number in order to delete it using "erase member function". Any help would be appreciated!

Desired Output:

StudentID StudentName Grade

5 bob 80.0

2 joe 60.5

Please enter your command: add

Please enter StudentID, StudentName and Grade: 2 joe 60.5 StudentID already exists!

Please enter your command: add

Please enter StudentID, StudentName and Grade: 3 jim 100.3 Grade out of range!

Please enter your command: add

Please enter StudentID, StudentName and Grade: 3 jim 90.3

Success!

Please enter your command: remove

Please enter a StudentID to remove: 97

StudentID doesn’t exist!

Please enter your command: remove
Please enter a StudentID to remove: 2

Success!

Please enter your command: print

StudentID StudentName Grade

5 bob 80.0

3 jim 90.3

Please enter your command: sort

Please indicate sort by StudentID or Grade? StudentID

StudentID StudentName Grade

3 jim 90.3

5 bob 80.0

Please enter your command: sort

Please indicate sort by StudentID or Grade? Grade

StudentID StudentName Grade

5 bob 80.0

3 jim 90.3

Explanation / Answer

#include <iostream>
#include <string>
#include <vector>
using namespace std;

//GradeBook class definition
class GradeBook
{
public:
   GradeBook();
//Declare a constructor that has one parameter for each data member
GradeBook(int, string, double);
//Declare a set method for Student ID
void setStudentID(int);
//Declare a get method for Student ID
int getStudentID();
//Declare a set method for Student's name
void setStudentName(string);
//Declare a get method for Student's name
string getStudentName();
//Declare a set method for grade
void setStudentGrade(double);
//Declare a get method for grade
double getStudentGrade();
void displayGradeBook();
private:
//Declare a int data member for Student ID
int studentID;
//Declare a string data member for student name
string studentName;
//Declare a double data member for grade
double studentGrade;
}; //End Class GradeBook
// constructor initializes StudentID, StudentName and Grade
GradeBook::GradeBook(int identification, string name, double grade)
{
setStudentID(identification); //initializes ID Number
setStudentName(name); //initializes Student name
setStudentGrade(grade); //initializes grade
}//end GradeBook constructor
//function to set student ID
void GradeBook::setStudentID(int identification)
{
studentID = identification; //store the ID
}//end function setStudentID
//function to retrieve the student ID
int GradeBook::getStudentID()
{
return studentID;
}//end function getStudentID
//function to set student name
void GradeBook::setStudentName(string name)
{
studentName = name; //store the name
}//end function setStudentName
//function to retrieve the student name
string GradeBook::getStudentName()
{
return studentName;
}//end function getStudentName
//function to set student grade
void GradeBook::setStudentGrade(double grade)
{
studentGrade = grade; //store the grade
}//end function setStudentGrade
//function to retrieve the student grade
double GradeBook::getStudentGrade()
{
return studentGrade;
}//end function getStudentGrade
void GradeBook::displayGradeBook()
{
//Display existing ID,name,and grade information already stored
cout << getStudentID() << " " << getStudentName() << " " << getStudentGrade() << endl;
}//end function displayGradeBook

int AddStudent(GradeBook gradeBooks[],int n);
int RemoveStudent(GradeBook gradeBooks[],int n);
void SortStudent(GradeBook gradeBooks[],int n);
void PrintStudent(GradeBook gradeBooks[],int n);
int getOption(string command);

int main()
{
cout << " Student ID Student Name Grade" << endl;
// create a GradeBook object; pass a course name and instructor name
GradeBook gradeBooks[10];
gradeBooks[0].setStudentID(3);
gradeBooks[0].setStudentName("Jim");
gradeBooks[0].setStudentGrade(97.3);
gradeBooks[1].setStudentID(2);
gradeBooks[1].setStudentName("Tom");
gradeBooks[1].setStudentGrade(77.5);
gradeBooks[2].setStudentID(1);
gradeBooks[2].setStudentName("Joe");
gradeBooks[2].setStudentGrade(66.9);
gradeBooks[3].setStudentID(5);
gradeBooks[3].setStudentName("Bob");
gradeBooks[3].setStudentGrade(88.7);
gradeBooks[4].setStudentID(4);
gradeBooks[4].setStudentName("Lee");
gradeBooks[4].setStudentGrade(83.2);
int n=5;
string command;
cout << "5 Available Commands Are:" << endl;
cout << "Add, Remove, Sort, Print, Quit" << endl;
cout << "Please Enter Your Command: ";
cin >> command;
int option=getOption(command);
switch(option)
{
    case 1: n=AddStudent(gradeBooks,n); break;
    case 2:n=RemoveStudent(gradeBooks,n);break;
    case 3: SortStudent(gradeBooks,n);break;
    case 4:PrintStudent(gradeBooks,n);break;
    case 5: exit(0);
}
return 0; // indicate successful termination
}//end main

bool found(GradeBook gradeBooks[],int id,int n)
{
   bool flag=false;
   for(int i=0;i<n;i++)
       {
   if(gradeBooks[i].getStudentID()==id)
          {
          flag=true;
           break;  
           }
   }
   return flag;
}
int AddStudent(GradeBook gradeBooks[],int n)
{
   int id;
   string name;
   double grade;
   bool flag=true;
   while(flag==true)
   {
   cout<<endl<<"Please enter StudentID, StudentName and Grade: ";
   cin>>id;
   cin>>name;
   cin>>grade;
   if(found(gradeBooks,id,n))
   {
       cout<<endl<<"StudentID already exists!";
       flag=true;
   }
   else{
       if(grade>100)
       {
       cout<<endl<<"Grade out of range!";
       flag=true;  
       }
       else
       {
       gradeBooks[n].setStudentID(id);
        gradeBooks[n].setStudentName(name);
        gradeBooks[n].setStudentGrade(grade);
        cout<<endl<<"Success!";
        flag=false;
        n++;  
        break;
       }
   }
}
return n;
}

int RemoveStudent(GradeBook gradeBooks[],int n)
{
   int id;
   bool flag=true;
   while(flag==true)
   {
   cout<<endl<<"Please enter a StudentID to remove: ";
   cin>>id;
   if(!found(gradeBooks,id,n))
   {
       cout<<endl<<"StudentID not exist!";
       flag=true;
   }
   else{
       int pos;
       for(int k=0;k<n;k++)
       {
           if(gradeBooks[k].getStudentID()==id)
           {
               pos=k;
               break;
           }
       }
       for(int m=pos+1;m<n;m++)
       {
           gradeBooks[m-1]=gradeBooks[m];
       }

        cout<<endl<<"Success!";
        flag=false;
        n--;  
        break;
   }}
return n;
}

int getOption(string command)
{
   int option;
   if(command.compare("Add")==0)
   {
       option=1;
   }
   else if(command.compare("Remove")==0)
   {
       option=2;
   }
   else if(command.compare("Sort")==0)
   {
       option=3;
   }
   else if(command.compare("Print")==0)
   {
       option=4;
   }
   else if(command.compare("Quit")==0)
   {
       option=5;
   }
   return option;
}
void SortStudent(GradeBook gradeBooks[],int n)
{
   int opt;
   cout<<endl<<"Please indicate sort by StudentID or Grade? StudentID <1/2>";
   cin>>opt;
   if(opt==1)
   {
       for(int i=0;i<n;i++)
       {
       for(int j=0;j<n;j++)
           {
              if(gradeBooks[i].getStudentID()>gradeBooks[j].getStudentID())
              {
                  int t=gradeBooks[i].getStudentID();
                  gradeBooks[i].setStudentID(gradeBooks[j].getStudentID());
                  gradeBooks[j].setStudentID(t);
                  string temp=gradeBooks[i].getStudentName();
                  gradeBooks[i].setStudentName(gradeBooks[j].getStudentName());
                  gradeBooks[j].setStudentName(temp);
                  double g=gradeBooks[i].getStudentGrade();
                  gradeBooks[i].setStudentGrade(gradeBooks[j].getStudentGrade());
                  gradeBooks[j].setStudentGrade(g);
           }
           }
       }  
   }
   else if(opt==2)
   {
       for(int i=0;i<n;i++)
       {
       for(int j=0;j<n;j++)
           {
              if(gradeBooks[i].getStudentGrade()>gradeBooks[j].getStudentGrade())
              {
                  int t=gradeBooks[i].getStudentID();
                  gradeBooks[i].setStudentID(gradeBooks[j].getStudentID());
                  gradeBooks[j].setStudentID(t);
                  string temp=gradeBooks[i].getStudentName();
                  gradeBooks[i].setStudentName(gradeBooks[j].getStudentName());
                  gradeBooks[j].setStudentName(temp);
                  double g=gradeBooks[i].getStudentGrade();
                  gradeBooks[i].setStudentGrade(gradeBooks[j].getStudentGrade());
                  gradeBooks[j].setStudentGrade(g);
           }
           }
       }  
   }
  
}

void PrintStudent(GradeBook gradeBooks[],int n)
{
   cout<<endl<<"StudentID StudentName Grade";
   for(int i=0;i<n;i++)
   {
       gradeBooks[i].displayGradeBook();
   }
}

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