1

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.

wkl
  • 77,184
  • 16
  • 165
  • 176
  • You probably want a database. – Gio Borje Jul 10 '12 at 02:04
  • 2
    Too vague. The most simple approach is obvious; write the data out to file. Of course, that doesn't scale well, so depending on your requirements you may want to look into a database. The important part of that last sentence was *"depending on your requirements..."* – Ed S. Jul 10 '12 at 02:04
  • 1
    You can use a flat text file, a database (like SQLite, which is useful for embedded data), some configuration format of your own choosing, or even using [Boost to read/write configuration files](http://stackoverflow.com/questions/6175502/how-to-parse-ini-file-with-boost). – wkl Jul 10 '12 at 02:04
  • I know this is too vague to qualify as hand-holding, but in terms of the intention of the question and the recommended approaches, it's almost a duplicate: http://stackoverflow.com/questions/7271307/interacting-with-a-simple-database-in-c – jogojapan Jul 10 '12 at 02:04
  • 1
    You may wish to include a target platform in your question (e.g. Linux, Mac, Windows...). There are usually OS-specific libraries for saving simple settings, and also conventions to follow. – Kevin Grant Jul 10 '12 at 02:07
  • Is this a command line application, a web app, or what? – ThomasMcLeod Jul 10 '12 at 02:13

1 Answers1

2

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

reagan
  • 653
  • 1
  • 4
  • 16