Ok, I have a load and save system set up for a small console based game I'm making to learn more about c++. So far, I have figured out how to write to a file and then load all the correct integers, but I can't re-assign those loaded integers to the program. If thats confusing, look at this code,
if (ans == 3){
system("cls");
cout<<"----------------------------------------"<<endl;
cout<<"Saving your data"<<endl;
cout<<"----------------------------------------"<<endl;
//Creates an instance of ofstream, and opens example.txt
ofstream a_file ( "pop.txt" );
// Outputs to pop.txt through a_file
if (a_file.is_open()){
a_file<<"SAVED INFORMATION"<<endl;
a_file<<"Male: "<<male<<endl;
a_file<<"Female: "<<female<<endl;
a_file<<"Total: "<<total<<endl;
a_file<<"Day: "<<day<<endl;
// Close the file stream explicitly
a_file.close();
system("pause");
return 0;
//yay it works! save and quit
}
else{
cout<<"Save unseccussful"<<endl;
}
}
if (ans == 4){
//load *doesnt work*
system("cls");
cout<<"----------------------------------------"<<endl;
cout<<"Loading saved data"<<endl;
cout<<"----------------------------------------"<<endl;
ifstream a_file ("pop.txt" );
if (a_file.is_open()){
a_file >> male;
male = male;
a_file >> female;
female = female;
a_file >> total;
total = total;
a_file >> day;
day = day;
cout<<"Load successful!"<<endl;
system("pause");
}
else{
cout<<"Load unseccussful"<<endl;
system("pause");
}
}
I open the file, the program finds the correct line and loads the number, but it won't assign the number to the program. And yes, I know system is bad to use.
Thanks for helping!
Doesn't extract this properly I believe. You try to extract an integer from "SAVED INFORMATION:" and this aught to break the ifstream object. After extracting male, put the code: if (!a_file.good()) cout << "File is broken";
This will tell you if the load actually loaded something.
Seems like this would be more appropriate for your file: