Background information:
- member is a vector of pointers to Student objects
- m is a student object
- I am trying to add pointers that point to a Student object as long as the name for that Student object has not been added already.
In my add_member program in StudentClub.cpp, here are the steps I took. There's clearly something wrong with the incremementation and creating the member vector but I can't figure out what it is.
1. I created a for loop to check each element of the vector against the name of the member object (the name is collected using getline in the main function) to see if there are any matches.
2. If there is a match, this means that the name has already been recorded so the function can be ended (so I put a return)
3. If there is no match, then the object can be pushed back into the member vector since there is no repeat and they are a new member.
I'm sure the mistake is in the definition of the function, but I also included a part of my main function. If you need any more information, just let me know!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
//This is the function declaration of add_member in StudentClub.cpp
#include "StudentClub.h"
#include <iostream>
#include <vector>
#include "Student.h"
using namespace std;
void StudentClub::add_member(Student* m)
{
for (int i = 0; i < member.size(); i++)
{
if (m->name == member[i]->name)
{
return;
}
}
member.push_back(m);
}
| |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
|
//This is the main function
#include <iostream>
#include "Student.h"
#include "StudentClub.h"
#include <vector>
#include <iomanip>
using namespace std;
int main()
{
Student p,v,s,t,m;
vector <Student*> clubmem;
StudentClub club (&p, &v, &s, &t, clubmem);
cout << "President: ";
getline (cin,p.name);
cout << endl;
cout << "Vice-President: ";
getline (cin,v.name);
cout << endl;
cout << "Secretary: " ;
getline (cin,s.name);
cout << endl;
cout << "Treasurer: ";
getline (cin,t.name);
cout << endl;
//if Q is typed by the user, then the function is exited
while(m.name != "Q")
{
cout << "New Member (Q to quit): ";
getline(cin, m.name);
cout << endl;
if (m.name != "Q")
{
club.add_member(&m);
}
}
club.add_member(&p);
club.add_member(&v);
club.add_member(&s);
club.add_member(&t);
| |