Search code examples
c++settingsapplication-settings

Persistent data/settings in C++ application


For the first time, I am writing a fairly complex application, which will remember the user's name, gender, age, etc. Here is what I need to happen:

  • The application starts, and it checks to see if the information (name, age, etc) has been saved.
    • If not, the application gets this info and saves it.

When the user restarts this program, the program should see that the information is saved, and will save this information as C++ variables.

I can give more information if you don't know what I want. I have never done this before, and after searching the internet for a long time, I've only become more confused, so I'm afraid I need some serious hand holding, sorry.

Thanks,

-Chris.


Solution

  • It looks like you're new to C++, so I would recommend saving the information to a file. (that is where my coursework started, not with databases)

    something like this should do the trick:

    #include <iostream>
    #include <fstream>
    #include <string>
    int main(){
        ifstream input("your_saved_file.txt");
        string name="", age="", etc="";
        input >> name >> age >> etc;
        if(name=="" || age=="" || etc==""){
            input.clear(); input.close();
            ofstream output("your_saved_file.txt");
            cout << "Enter name: ";
            cin >> name;
            cout << "Enter age: ";
            cin >> age;
            cout << "Enter etc: ";
            cin >> etc;
            output << name << endl << age << endl << etc << endl;
            output.close(); output.clear();
        }
        else{
            input.clear(); input.close();
            cout << "Name: " << name << " Age: " << age << " Etc: " << etc;
        }
        return 0;
    }
    

    hope that's the kind of thing you're looking for,

    reagan