Hello, I'm having trouble in getting my program to read from a file and put all the proper data into its proper class variables. I have a class (called Champion) that has string variable for a name and a vector of strings for items. I also have a vector of Champion that holds multiple champions. Any help would be appreciated. Here's my code:
In looking at your save routine, the problem I see is that you don't write a delimiter between each champion (or a count of how many items each champion has). This makes it impossible to determine where one champion's data ends and the next one starts.
Lines 94-96: You want to read the champion's name first, then you want to loop reading that champion's items until you either encounter a delimiter, or reach the number of items that champion has.
I would suggest adding at line 103 of champion.cpp:
stream << c.numOfItems() << endl;
Then on load, after reading the champion's name, read the number of items from the file, then loop reading items for that number of times.
You have already overloaded the << operator to write a champion to an output stream. Why not overload the >> operator to read a champion from an input stream.
Lines 93-97 become simply:
93 94 95 96
Champion champ; // temp instance used for reading
while (ifs >> champ)
champions.push_back (champ);
ifs.close();
This encapsulates the code for reading a champion into it's class where it belongs. This does not address the delimiter/count issue, but it isolates it to code in your class. If you decide to add additional variables to champion, only your input and output overloads needs to change.