Saving user data for subsequent runs of a program.

  • Thread starter Thread starter Danzellen
  • Start date Start date
D

Danzellen

Guest
So I'm currently trying to find a way to save user data (which consists of some integers, maps of tm values, and maps of class values), but I'm not sure how to go about it. One method I tried was saving data to a data file:

int main()
{
string name;
int age;
bool test = false;
string input;
ofstream saveData;
ifstream takeData;
std::time_t tt = system_clock::to_time_t(system_clock::now());
tm curtime = *localtime(&tt);
tm time = curtime;
takeData.open("data.dat");
takeData >> name;
takeData.close();
if (name == "") {
name = "blank";
}
cout << "Name is " << name << endl;
takeData.open("data2.dat");
takeData >> age;
takeData.close();
if (age < 0) {
age = 0;
}
cout << "Age is " << age << endl;
takeData.open("data3.dat");
takeData >> test;
takeData.close();
if (test == true) {
cout << "True." << endl;
} else {
cout << "False." << endl;
}
takeData.open("data4.dat");
takeData >> time;
takeData.close();
cout << "The input time is " << asctime(&time);
cout << "Your name?" << endl;
cin >> name;
saveData.open("data.dat");
saveData << name;
saveData.close();
cout << "Your Age?" << endl;
cin >> age;
saveData.open("data2.dat");
saveData << age;
saveData.close();
cout << "Is it true? (y/n)" << endl;
cin >> input;
if (input == "y") {
test = true;
} else {
test = false;
}
cout << "Setting time to december 1st." << endl;
time.tm_mon = 11;
time.tm_mday = 1;
saveData.open("data4.dat");
saveData << time;
saveData.close();
return 0;
}

This was just a test, and it seemed to be working until I used tm values, at which point I got the error:

Error C2679 binary '>>': no operator found which takes a right-hand operand of type 'tm' (or there is no acceptable conversion)

The data I'm try to save is:

map<string, Date*> m_dates;
map<string, Tracker*> m_tasks;
map<string, vector<tm>> m_taskreports;
map<string, tm> m_datereports;
map<string, Goal*> m_goals;

Along with 4 integers. Is there a simple way I could save this data when the user closes the program and then restore it when they open it up again?

Continue reading...
 
Back
Top