My game is a sort of RPG with stats, money etc. I just recently added a save/load system using writing to a file. It was kinda over my head and skill level but I managed to get bits of code here and there to get it to work.
Anyway. I initially got writing to a text file to work, then I got loading to work too.I eventually was able to read numbers from the file and assign them to integer variables in order.
My issue was I wanted to check if a save file existed, if it did, load it up, if it did not, go to character creation. I had a lot of trouble with this and after trying different code snippets to work I finally got it to check if a file existed, and execute the appropriate code.
My issue now is my code USED to go through each entry and assign variables in order.
Like the first number in the text file would be for the variable money, and it would read it, assign to to int money and scroll to down to the next variable for player strength, assign to to playerstr variable and so on. After making the tweak for loading it no longer functions like this, and makes the last entry in the text file the value for everything.
Can anyone help me out here? I've very new, so explain like I'm five. :)
Here's my code:
Save Code:
1 2 3 4 5 6 7 8 9
|
{
ofstream savegame;
savegame.open("C:/Sounds/savegame.dat", ios::trunc);
savegame << money << endl;
savegame << playerstr <<endl;
savegame.close();
cout << "Saved game./n";
goto mainmenu;
}
|
Load Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
ifstream savegame("c:/sounds/savegame.dat"); // initialize savegame.dat file
std::string data; // intialize data integer
//load savegame
if (std::ifstream("c:/sounds/savegame.dat"))
{
while(! savegame.eof()) {
getline(savegame, data);
stringstream(data) >> money;
stringstream(data) >> playerstr;
std::cout << data << "\n\n";
}
std::cout << "File Loaded" << std::endl;
goto mainmenu;
}
|
I barely get how this code works, how can I tweak it to go through the file in order and assign variables one at a time?
At the current moment, it assigned the playerstr value to both money and playerstr int. But the save file being created lists the correct values in order.