Question: How do I test this function in my main program. I want to be able to display all students. I tried student1.listStudents(History); however it only

How do I test this function in my main program. I want to be able to display all students.

I tried student1.listStudents("History"); however it only passes the one student

My main cpp has these objects declared :

Student student1(1001, "Mary", "Smith", "Computer Science", 3.1);

Student student2(1002, "Emma", "Greek", "Education", 4.0);

Student student3(1003, "John", "Salvator", "Biology", 3.9);

Student student4(1004, "Ricky", "Delhunty", "History", 3.8);

Student student5(1005, "Olive", "Jones", "Computer Science", 1.7);

Function to test:

void Student::listStudents(string major) {

vector students;

students.push_back(*this);

bool found = 0;

for (int i = 0; i < students.size(); i++) {

if (students[i].getMajor() == major)

{

cout << "Student ID: " << students[i].getId() << endl;

cout << " Name: " << students[i].getFirstName() << " " << students[i].getLastName() << endl;

cout << " Major: " << students[i].getMajor() << endl;

cout << " GPA: " << students[i].getGpa() << endl;

found = 1;

}

}

if (found == 0)

{

cout << "No students with a " << major << " major were found" << endl;

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!