Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

c++ Why does my code keep stopping after inserting an object in a vector?

this is literally my final please help. my code keeps stopping and I'm not sure why?

vector<Student>students;
Student *s[20];
int i = 0;
char answer, answer2;
cout << "Add new student? [y/n] ";
cin >> answer;
while (answer == 'Y' || answer == 'y'){
    cout << "Are they a student worker? [y/n] ";
    cin >> answer2;
    if (answer2 == 'Y' || answer2 == 'y'){
        StuWorker *sw = new StuWorker();
        sw->read();
        s[i] = sw;
        i++;
        students.push_back(*sw);
    }
    else{
        Student *sdt = new Student();
        sdt->read();
        s[i] = sdt;
        i++;
        students.push_back(*sdt);
    }
    cout << "\nAdd new student? [y/n]";
    cin >> answer;
}

Please help me understand why my code isn't working. I was taught to dereference the pointer was to do so as above.

I'm honestly just typing other stuff because stack overflow refuses to let me post this unless I type other stuff.

UPDATE: StuWorker is a class derived from Student class
Also, after the second iteration, my code stops
Also also, when I asked my professor for help about push_back all he sent was this:

Vector<Student> v; //just an example   
Student *pointer_to_student = new Student;   
V.push *pointer_to_student;   

Here is the first part of his instructions, word by word. If there is something I'm not understanding from these instructions please tell me:
In main(), create a vector of Student objects and keep asking the user if he or she wants to add a Student. If the answer is 'y' or 'Y', ask if the student being added is a student worker. If it is, allocate memory for a StuWorker and if not, allocate memory for a Student, and in each case, add it to an array of Student pointers of maximum size 20 (not directly into the vector). For each Student or StuWorker dynamically created, call the object's read() function to read data from the user. This should be okay since a student worker is a Student, too and depending on the type of object that's been created, the correct class's read() is called (this is the polymorphism part). As each object is created and data read into it, push it into the vector declared earlier. Continue as long as the user answers with 'y' or 'Y' to "Add student?" question.

this is read() for Student:

void Student::read(){
cout << "\nEnter Student's name: ";
cin.ignore();
getline(cin, name);
cout << "Enter Student's ID: ";
getline(cin, idN);
cout << "Enter units completed by Student: ";
cin >> units;
cout << "Enter student's GPA: ";
cin >> gpa;

}

and this is read() for StuWorker:

void StuWorker::read(){
    Student::read();
    cout << "Enter hours assigned per week: ";
    cin >> hours;
    cout << "Enter hourly rate: ";
    cin >> hourlyRate;
}

Comments