I am making a C++ Command window Text Adventure, and I want it to save what number variables are currently set at. So if the player saves when they have an enchantment level of 5, the next time they go on the program and load a .dat or a save file, their enchantment level stays at five, and they will be at the same location they were when they saved.
Simply use file stream! First determine in which order are the saved variables, and then if the user wishes to save his/her progress, save the data in that order, and the next time the application is launched, prompt the user does he/she want to load an already saved file, and if so, load the data from the file in the corresponding variables.
//your includes...
//your usings
//your functions
int main()
{
/*your variables that the player changes automaticaly
during the course of the game*/
cout<<"Load a saved file or start a new game?(L/N)\n";
char choice;
cin>>choice;
if (choice=='L')
{
string file;
cout<<"Enter the name of your file: \n";
cin>>file;
ifstream fin((file+".save").c_str(), ios::binary);
//input your variables
}
//play the game
cout<<"You chose to exit. Do you wish to save your current progress\n\
to continue the game later?(Y/N)";
cin>>choice;
if (choice=='Y')
{
string save;
cout<<"Enter the name of the file in which you want to save your progress: \n";
cin>>save;
ofstream fout((save+".save").c_str(), ios::binary);
//output all the variables that determine a player's progress in the file.
}
//end the game
}
No, you have to enter them through the stream! Well, in theory you could, but that's too complicated! Just fin>> them at the beginning, and fout<< them at the end.