-
save/load problem
It's saving to the .txt file, but not loading anything.
Code:
void SaveGame (void)
{
ofstream fout;
fout.open("XFstats.txt");
fout << "Do not change or edit the contents of this file. "<<endl<<endl<<endl;
fout << kills;
fout << "Health: "<< yourhealth;
fout << "Weapons: " << crossbow << axe << bronzesword
<< silversword << goldsword << platinumsword
<< flamethrower << rocketlauncher << endl;
fout << "Money: " << money << endl;
fout << "Difficulty: " << easy << hard << endl;
fout.close();
cout<<endl<<"Game saved successfully!"<<endl;
getch();
repeat();
}
void LoadGame (void)
{
ifstream fin;
char szWord[] = "";
fin.open("XFstats.txt");
if(fin.fail())
{
cout << "ERROR: Could not find XFstats.txt!\n";
}
fin.seekg(NULL, ios::beg);
fin.clear();
fin >> szWord >> kills;
fin >> szWord >> yourhealth;
fin >> szWord >> crossbow >> axe >> bronzesword
>> silversword >> goldsword >> platinumsword
>> flamethrower >> rocketlauncher;
fin >> szWord >> money;
fin >> szWord >> easy >> hard;
fin.close();
cout<<endl<<"Game loaded successfully!"<<endl;
getch();
repeat();
}
-
You never allocated any memory for szWord.
char pszWord[128]; will do it.
-
Not only that, but you're overwriting each call by the next. Perhaps you should use separate variables or else use strcat to add each one to the buffer. Better yet, just read in the whole file into a buffer in one call...
-
here was my monster save/load function from my old RPG. maybe you can get something from it. i think i did the searching loops pretty clever though :) since you cant save/load a pointer i had to convert them into integers and convert them back on loading....
Code:
case 10:
ofstream save;
ifstream load;
int choice;
int weapon_id=0; //for save function
int scroll1_id=0;
int scroll2_id=0;
int scroll3_id=0;
int room_id=0;
ROOM* search_room;
SCROLL* ptemp_room;
WEAPON* ptemp_room1;
int room_scroll_id;
cout<<"1. Save"<<endl;
cout<<"2. Load"<<endl;
cout<<"3. Back"<<endl;
cout<<"Selection: ";
cin>>choice;
if(choice==1)
{
cout<<endl<<"Saving"<<flush;
Sleep(400);
cout<<"."<<flush;
Sleep(400);
cout<<"."<<flush;
Sleep(400);
cout<<"."<<flush;
Sleep(400);
cout<<"."<<flush;
Sleep(400);
cout<<"."<<flush;
Sleep(400);
cout<<"."<<flush;
cout<<"(Hit any key to continue)"<<flush;
save.open("player.lee", ios::trunc);
save<<main.name<<"\n";
save<<profession<<"\n";
save<<main.health<<"\n";
save<<main.max_health<<"\n";
save<<main.speed<<"\n";
save<<main.strength<<"\n";
save<<main.magic<<"\n";
save<<main.energy<<"\n";
save<<main.max_energy<<"\n";
save<<main.lvl<<"\n";
save<<main.exp_needed<<"\n";
save<<main.gold<<"\n";
save<<main.scrollcount<<"\n";
save<<main.to_hit<<"\n";
save<<main.ac<<"\n";
save<<level_up_count<<"\n";
save<<current_weapon->id<<"\n";
save<<current_room->id<<"\n";
if(current_scroll1!=NULL)
save<<current_scroll1->id<<"\n";
else
save<<0<<"\n";
if(current_scroll2!=NULL)
save<<current_scroll2->id<<"\n";
else
save<<0<<"\n";
if(current_scroll3!=NULL)
save<<current_scroll3->id<<"\n";
else
save<<0<<"\n";
search_room=&chamber;
do
{
save<<search_room->flag<<"\n";
search_room=search_room->next;
}while(search_room!=&chamber);
search_room=&chamber;
do
{
ptemp_room=search_room->scroll;
if(ptemp_room!=NULL)
{
room_scroll_id=ptemp_room->id;
save<<ptemp_room->id<<"\n";
}
else if(ptemp_room==NULL)
{
save<<0<<"\n";
}
ptemp_room1=search_room->weapon;
save<<ptemp_room1->id<<"\n";
search_room=search_room->next;
}while(search_room!=&chamber);
}
else if(choice==2)
{
cout<<endl<<"Loading"<<flush;
Sleep(400);
cout<<"."<<flush;
Sleep(400);
cout<<"."<<flush;
Sleep(400);
cout<<"."<<flush;
Sleep(400);
cout<<"."<<flush;
Sleep(400);
cout<<"."<<flush;
Sleep(400);
cout<<"."<<flush;
cout<<"(Hit any key to continue)"<<flush;
load.open("player.lee");
int count=1;
int id[1000];
SCROLL* search_scroll;
WEAPON* search_weapon;
load>>main.name;
load>>profession;
load>>main.health;
load>>main.max_health;
load>>main.speed;
load>>main.strength;
load>>main.magic;
load>>main.energy;
load>>main.max_energy;
load>>main.lvl;
load>>main.exp_needed;
load>>main.gold;
load>>main.scrollcount;
load>>main.to_hit;
load>>main.ac;
load>>level_up_count;
load>>weapon_id;
load>>room_id;
load>>scroll1_id;
load>>scroll2_id;
load>>scroll3_id;
search_room=&chamber;
do
{
load>>search_room->flag;
search_room=search_room->next;
}while(search_room!=&chamber);
search_room=&chamber; //Loading which scroll IDs are in which rooms
do
{
load>>id[count];
count++;
load>>id[count];
count++;
search_room=search_room->next;
}while(search_room!=&chamber);
for(int i=1;i<=(count-1);i+=2) //Scrolls in the rooms
{
search_scroll=&fireball;
do
{
if(id[i]==search_scroll->id)
search_room->scroll=search_scroll;
else if(id[i]==0)
search_room->scroll=NULL;
search_scroll=search_scroll->next;
}while(search_scroll!=&fireball);
search_room=search_room->next;
}
search_room=&chamber;
for(i=2;i<=(count);i+=2)
{
search_weapon=†
do
{
if(id[i]==search_weapon->id)
search_room->weapon=search_weapon;
search_weapon=search_weapon->next;
}while(search_weapon!=&dagger);
search_room=search_room->next;
}
search_weapon=†
do
{
if(search_weapon->id==weapon_id)
current_weapon=search_weapon;
search_weapon=search_weapon->next;
}while(search_weapon!=&dagger);
search_room=&chamber;
do
{
if(search_room->id==room_id)
current_room=search_room;
search_room=search_room->next;
}while(search_room!=&chamber);
search_scroll=&fireball;
do
{
if(search_scroll->id==scroll1_id)
current_scroll1=search_scroll;
if(search_scroll->id==scroll2_id)
current_scroll2=search_scroll;
if(search_scroll->id==scroll3_id)
current_scroll3=search_scroll;
search_scroll=search_scroll->next;
}while(search_scroll!=&fireball);
}
}
-
sleep and loading
Can anyone here give an example, a complete example on how to use sleep properly, and, a full example, with comments on how to load a file? thx