I have an assignment to use a previously created class and vector to take names as input, create and assign them to a new pointer, and then store them in the vector. I have been trying it for days and can't come up with the correct syntax.
This is the code I am given to start with and the exact instructions:
"With each name, create a Student pointer and insert it into the "students" vector. You must print all the names in the vector in reverse order."
For the most part it's okay, just a few notes:
* The compiler will reject line 12. I'll let you figure out why.
* Don't forget to release the pointers in the vector.
* 'names' is not a good name for a variable that will hold exactly one name at any time.
I think I have fixed the things pointed out by helios but I'm still having issues when I try and print the vector. I think it just returns the same memory address for each spot in the vector.
You can print std::strings using std::cout <<, but note that your vector stores pointers to std::strings, not the std::strings themselves. You need to dereference the pointers in order to print the strings.
Also, now you're releasing the pointers too early. If you to do what I said in the previous paragraph, the program will crash.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
std::vector<int *> v;
int input;
// Allocation phase
do{
std::cin >> input;
v.push_back(newint(input));
}while(input != 0);
// Usage phase
for (size_t i = 0; i < v.size(); i++)
std::cout << *v[i] << std::endl;
// Deallocation phase
for (size_t i = 0; i < v.size(); i++)
delete v[i];