URGENT Help Needed: This is a link to the problem I have to do: http://imgur.com
ID: 3674533 • Letter: U
Question
URGENT Help Needed:
This is a link to the problem I have to do: http://imgur.com/a/o1qPx
My code has objects with a student ID (int), student name (string), student grade (double). I initialized 5 objects and stored them in a vector. So far, I got my code to keep adding user input objects until the user stops and also worked on removing a certain object until I got stuck.
I have to Initialize 5 objects from a class and store it in a vector. Then, ask the user if they want to add, remove, sort, print or quit. If they say add, I have to take the user inputs and create an object and store it in the vector. If they say remove, I have to ask for a student ID, find the student Id they enter and then remove that object. This compiles in my code but does not operate correctly. I also do not know how to do the sort function. And I tried making a switch statement where I could make the code perform the operation the user wanted but got stuck there too and it did not work. If someone could help me through the rest of what I have to do, I would be very thankful. I have been trying to do this for about 6 hours but I do not seem to quite understand how I'm supposed to do this so i'm having a lot of trouble. If the directions are unclear, I posted a link above to screenshots of the outputs I'm supposed to be getting and also the complete details of what I have to do. Thank you for your help in advance!
Here is my code:
#include <iostream>
#include <vector>
#include <string>
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();
//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
int ID;
string Name;
double Grade;
string command;
int userinput;
vector <GradeBook> vec;
GradeBook gradeBook1(1, "Bob", 72.3);
vec.push_back(gradeBook1);
GradeBook gradeBook2(4, "Jim", 85.4);
vec.push_back(gradeBook2);
GradeBook gradeBook3(2, "Rob", 99.6);
vec.push_back(gradeBook3);
GradeBook gradeBook4(3, "Ron", 89.7);
vec.push_back(gradeBook4);
GradeBook gradeBook5(5, "Jon", 66.9);
vec.push_back(gradeBook5);
vector <GradeBook>::iterator it;
for (it = vec.begin(); it != vec.end(); ++it) {
it->displayGradeBook();
}
cout << " Five Available Commands Are:" << endl;
cout << "Add, Remove, Sort, Print, Quit" << endl;
//cout << "Enter Your Command: " << endl;
//cin >> command;
int n = 0;
while (n<=30000){
cout << "Enter Student ID: ";
cin >> ID;
cout << "Enter Student Name: ";
cin >> Name;
cout << "Enter Student Grade: ";
cin >> Grade;
GradeBook gradeBook(ID,Name,Grade);
vec.push_back(gradeBook);
vector <GradeBook>::iterator it2;
cout << " Student ID Student Name Grade" << endl;
for (it2 = vec.begin(); it2 != vec.end(); ++it2) {
it2->displayGradeBook();
}
n++;
cout << "Enter the Student ID You Want to Remove: ";
cin >> userinput;
vector <GradeBook>::iterator it3;
cout << " Student ID Student Name Grade" << endl;
for (it3 = vec.begin(); it3 != vec.end(); ++it3) {
if ( it3->studentID == userinput ){
vec.erase(vec.begin()+userinput);
}
else
{
cout << "";
}
it3->displayGradeBook();
}
}
return 0; // indicate successful termination
}
Explanation / Answer
#include <iostream>
#include <vector>
#include <string>
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();
//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
int ID;
string Name;
double Grade;
string command;
int userinput;
vector <GradeBook> vec;
GradeBook gradeBook1(1, "Bob", 72.3);
vec.push_back(gradeBook1);
GradeBook gradeBook2(4, "Jim", 85.4);
vec.push_back(gradeBook2);
GradeBook gradeBook3(2, "Rob", 99.6);
vec.push_back(gradeBook3);
GradeBook gradeBook4(3, "Ron", 89.7);
vec.push_back(gradeBook4);
GradeBook gradeBook5(5, "Jon", 66.9);
vec.push_back(gradeBook5);
vector <GradeBook>::iterator it;
for (it = vec.begin(); it != vec.end(); ++it) {
it->displayGradeBook();
}
cout << " Five Available Commands Are:" << endl;
cout << "Add, Remove, Sort, Print, Quit" << endl;
//cout << "Enter Your Command: " << endl;
//cin >> command;
int n = 0;
while (n<30000){
cout << "Enter Student ID: ";
cin >> ID;
cout << "Enter Student Name: ";
cin >> Name;
cout << "Enter Student Grade: ";
cin >> Grade;
GradeBook gradeBook(ID,Name,Grade);
vec.push_back(gradeBook);
vector <GradeBook>::iterator it2;
cout << " Student ID Student Name Grade" << endl;
for (it2 = vec.begin(); it2 != vec.end(); ++it2) {
it2->displayGradeBook();
}
n++;
cout << "Enter the Student ID You Want to Remove: ";
cin >> userinput;
vector <GradeBook>::iterator it3;
cout << " Student ID Student Name Grade" << endl;
for (it3 = vec.begin(); it3 != vec.end(); ++it3) {
if ( it3->studentID == userinput ){
vec.erase(vec.begin()+userinput);
}
else
{
cout << "";
}
it3->displayGradeBook();
}
}
return 0; // indicate successful termination
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.