In MY text-based rpg (unfinished, but playable), I define the struct and declare instances of the struct globally, then in the function that creates a character, it saves values to an instance of the struct depending on the class you chose. Here's my code...
Code:
struct statistics {
string nam; //Name
int lev; //Level
int att; //Attack
int def; //Defense
int tempdef; //To heighten defense for a turn.
int agi; //Agility (not currrently used in the program)
int mon; //Money
int sta; //Stamina
int tempsta; //To keep track of player health while regenerating it after battle.
float gloc; //To keep track of your location in Story Mode.
float exp; //Experience (for leveling up, not currently used)
};
statistics player; //For players stats
statistics opponent; //For enemy stats during battle
And my createdata() function:
Code:
void createdata(string rac) {
if (rac == "d" || rac == "D")
{
string dnam;
ofstream char1("dnam.txt"); //Name
ofstream char2("dlev.txt"); //Level
ofstream char3("datt.txt"); //Attack
ofstream char4("ddef.txt"); //Defense
ofstream char5("dagi.txt"); //Agility
ofstream char6("dmon.txt"); //Money
ofstream char7("dexp.txt"); //Experience
ofstream char8("dsta.txt"); //Stamina
ofstream char9("dtst.txt"); //Stamina for use in battles;
//refills to value of sta after battle().
ofstream char10("dglo.txt"); //See gloc in struct statistics.
cout << endl << "What is your character's name? ";
cin >> dnam;
char1 << dnam;
char2 << "1"; //Starting level
char3 << "100"; //Starting attack
char4 << "95"; //Starting defense
char5 << "100"; //Starting agility
char6 << "0"; //Starting money
char7 << "1"; //Starting experience
char8 << "25"; //Starting stamina
char10 << "1"; //Starting game loc
char1.close();
char2.close();
char3.close();
char4.close();
char5.close();
char6.close();
char7.close();
}
else if (rac == "e" || rac == "E")
{
string enam;
ofstream char1("enam.txt"); //Name
ofstream char2("elev.txt"); //Level
ofstream char3("eatt.txt"); //Attack
ofstream char4("edef.txt"); //Defense
ofstream char5("eagi.txt"); //Agility
ofstream char6("emon.txt"); //Money
ofstream char7("eexp.txt"); //Experience
ofstream char8("esta.txt"); //Stamina
ofstream char9("etst.txt"); //Stamina for use in battles;
//refills to value of sta after battle().
ofstream char10("eglo.txt"); //See gloc in struct statistics.
cout << endl << "What is your character's name? ";
cin >> enam;
char1 << enam;
char2 << "1"; //Starting level
char3 << "50"; //Starting attack
char4 << "40"; //Starting defense
char5 << "65"; //Starting agility
char6 << "0"; //Starting money
char7 << "1"; //Starting experience
char8 << "20"; //Starting stamina
char10 << "1"; //Starting game loc
char1.close();
char2.close();
char3.close();
char4.close();
char5.close();
char6.close();
char7.close();
}
else if (rac == "h" || rac == "H")
{
//...
//You get the idea.