I have a class, User, with a member string name;. In main(), I declare vector<User*> userList; and add a few pointers to User objects with
1 2
userList.push_back(new User);
userList[userList.size() - 1]->name = "a name";
Later, I want to print out each User's name, so I tried cout << userList[i]->name;, and nothing prints. I know (or at least I think) that the dot operator is not appropriate here. What is the correct way to access these members?
Sorry! I forgot about that. I got a seg fault error and in my actual file I'd already fixed it using .back(), but forgot to put that in this forum. I still don't get anything when I try to print though.
I've discovered that the problem is when I get the name as input. I use getline(cin, tempdata), where tempdata is a name followed by an age. The reason I have to use getline is that if tempdata is "done", I break out of the function, so I cannot use cin >> tempname >> tempage.
After that, I use a for loop to find out how many characters are in tempdata before the space, and copy the name into tempname. I know this is where my problem is now, but can't figure out what it is.
1 2 3 4 5 6 7 8 9 10
for (int i = 0; i < tempdata.length(); i++) {
if (tempdata[i] == ' ') { // Check name's length
namelength = i;
break;
}
else {
tempname[i] = tempdata[i]; // Copy name
}
}
cout << tempname;