Hey guys, I can't figure out how to read a line into a vector of objects... I have a text file with lines such as:
Name Number
Name Number, etc. I want to read the file line by line, item by item so I can have a vector of objects with type Person. Each 'Person' would have private name and number variables.
Thanks for any help. I'm hoping to implement this with fstream
To read into a vector, I recommend using vectorname.push_back(object);
It expands the vector and adds the object onto the end. You'll want a temporary object to store data in for the push_back().
To do the actual reading... I recommend (assuming name is a std::string) using std::getline(stream, name, delimiting_character).
If your name and number are separated by a tab, that can allow the name to have spaces and other wonky characters.
Then, just do stream >> int to get the number (assuming number is an int). Create methods for these inside your class?