I have a struct called family which has a father and children (number of children are unknown). Then in add function I add the info of father and save everything in a vector myVec.
1 2 3 4 5 6
|
struct family{
string father;
string children;
vector <string> VecChildren; // I am storing children in a vector of string because number of children are unknown
};
vector <family> myVec; // I store whole struct in this vector.
|
Then I have the add function and there's a problem here.
Everytime I add children, it adds them properly for the firs time, but when I call the add function the second time, it adds the children from the first time and then adds the children I entered in second time.
Lets say that first time I entered children: 111, 222, 333.
So it adds them.
Then during second time, I enter children: 444, 555, 666.
This time, what it does it that it adds: 111, 222, 333, 444, 555, 666.
So the whole vector has "111, 222, 333" in first element and "111, 222, 333, 444, 555, 666" in the second element.
So How can I make it stop repeating previously added elements.
Below is my code of Function Add()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
family myNewFamily;
void Add()
{
cout << " name of father: ";
cin >> myNewFamily.father;
cout << " Enter children: ";
do{
cin << myNewFamily.children;
myNewFamily.children = lowerCase(myNewFamily.children); // a simple function i wrote to convert string into lowercase
myNewFamily.VecChildren.push_back(myNewFamily.children);
}while(myFamily.children != "zzz") // exit loop after user hits zzz
myVec.push_back(myNewFamily); // adding whole struct into myVec
}
|
SAMPLE OUTPUT
*** ADD MODE ***
Enter the name of the father: dad1
Enter it's children:
111
222
333
myVec[i].VecChildren[j] has:
int i = 0
111, 222, 333, ,
*** ADD MODE ***
Enter the name of the father: dad2
Enter it's children:
444
555
666
myVec[i].VecChildren[j] has:
int i = 0
111, 222, 333, ,
int i = 1
111, 222, 333, , 444, 555, 666, , |