There are a couple of problems I see with the rest of the code you posted, and they don't have to do with the way you set up your vector. First, you forgot the brackets for your for loop. Second, you put all[0] instead of all
, so it would push each one to the back of the zeroth element in all (assuming the problem with the for loop brackets is fixed). Here is the corrected code. See if this works for you, and if not, post your entire code, from start to finish, along with what error messages, if any, you are receiving.
1 2 3 4 5 6 7 8 9 10 11
|
void more()
{
string desc = "";
for (int i = 0; i < all.size(); i++)
{
std::cout << "Enter description of this person: ";
std::cin >> desc;
all[i].push_back(desc);
}
}
|
I also agree with jib; a class/struct would be far better suited for this situation than a vector of elements. Yes, you [i]can use a vector, but it is not the right tool for the job. I don't know why anyone would tell you not to use a struct for what it's built for, unless the point of this exercise is to show you how poorly suited a vector is for this task, and how much easier it is to use a struct, unless you actually
don't know how to use a struct (in which case, it is time to learn how to use it). If your advisor actually thinks using a vector to do this is
simpler than a struct, you should find a new advisor, because they are dead wrong. A struct allows you to access each element by name, whereas with a vector, you need to remember which element is stored at which index, and hope you didn't assign to the wrong one at some previous point in your code. You are only making things harder for yourself by using a hammer to drive a screw.