Hi.
I am making a text based rpg game (in C++) and am trying to get the game to save and load different characters.
Character is a class I defined. Basically, I have all of the class's data loaded into arrays, and then I output those arrays to the file.
Before, I had the program open the game file and save to it.
Now I want it to create a file based on the Character's name, and save to the new file.
The problem is, I don't know how to change file names or create files based upon a string variable.
Code:
void save(Character & You)
{
string Ystr[2], n1, n2;
string name;
char n[25];
int Yint[23], ar1[4], sw1[6], ite[4];
int z;
name = You.name;
name = "c:\\darkland\\" + name + ".out";
for(int i = 0; i<25; i++)
{
if(name.at(i) != ' ')
n[i] = name.at(i);
}
//tofile.open("c:\\darkland\\game.out");
//the above is my previous file
tofile.open(n);
system("CLS");
cout << "....Saving...." << endl;
You.name + ".out");
You.arrayStats(Ystr, Yint); //fills class arrays
You.Armor1.getvars(n1, ar1); //fills armor arrays
You.Sword1.getvars(n2, sw1); //fills sword arrays
You.Items.getvars(ite); //fills item arrays
//Global Vars Saved
tofile << checkpoint << endl;
tofile << north << endl;
tofile << east << endl;
tofile << Ystr[0] << endl; //saves character strings
tofile << Ystr[1] << endl;
for(z=0; z < 23; z++)
{
tofile << Yint[z] << endl; //saves character stats
}
tofile << n1;
for(z=0; z < 4; z++)
tofile << ar1[z]; //enters armor into file
tofile << n2;
for(z=0; z < 6; z++)
tofile << sw1[z]; // enters sword into file
for(z=0; z < 4; z++)
tofile << ite[z]; //enters items into file
tofile.close();
pause();
}