Thread: Is this save/load function more complicated than it needs to be?

  1. #1
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753

    Is this save/load function more complicated than it needs to be?

    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.txt", ios::trunc);
    
    				save<<main.name<<"\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.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.txt");
    
    				int count=1;
    				int id[1000];
    
    				SCROLL* search_scroll;
    				WEAPON* search_weapon;
    
    				load>>main.name;
    				load>>main.health;
    				load>>main.max_health;
    				load>>main.speed;
    				load>>main.strength;
    				load>>main.magic;
    				load>>main.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;
    				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)
    				{
    					search_scroll=&fireball;
    					do
    					{
    						if(id[count]==search_scroll->id)
    							search_room->scroll=search_scroll;
    						search_scroll=search_scroll->next;
    					}while(search_scroll!=&fireball);
    					search_room=search_room->next;
    				}
    
    				search_room=&chamber;
    				for(i=1;i<=(count-1);i+=2)
    				{
    					search_weapon=&dagger;
    					do
    					{
    						if(id[count]==search_weapon->id)
    							search_room->weapon=search_weapon;
    						search_weapon=search_weapon->next;
    					}while(search_weapon!=&dagger);
    					search_room=search_room->next;
    				}
    
    				search_weapon=&dagger;
    				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;
    					else if(search_scroll->id==scroll2_id)
    						current_scroll2=search_scroll;
    					else if(search_scroll->id==scroll3_id)
    						current_scroll3=search_scroll;
    					search_scroll=search_scroll->next;
    				}while(search_scroll!=&fireball);
    			}
    I am not able to save the addresses for pointers, or something like that. I forgot. But I ended up having to search for the id/flag numbers.

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    Code:
    Sleep(400);
    cout<<"."<<flush;
    Sleep(400);
    cout<<"."<<flush;
    Sleep(400);
    cout<<"."<<flush;
    Sleep(400);
    cout<<"."<<flush;
    Sleep(400);
    cout<<"."<<flush;
    Sleep(400);
    cout<<"."<<flush;
    These are redundant.. you could make a function like

    Code:
    void waitWithDots(int numDots) {
       for (int i = 0; i < numDots; i++) {
           Sleep(400);  
           cout << "." << flush;
       }
    }
    And then call waitWithDots(5);

    Also, I'd break up the loading/saving into seperate functions, so as to make the program more readable... it's easy to get lost from the fact that you are displaying a menu, when 95% of the code is dealing with file i/o.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    142
    that's a struct right?

    i know you could read the entire struct in one call,

  4. #4
    Registered User
    Join Date
    May 2002
    Posts
    317
    Code:
    code:--------------------------------------------------------------------------------
    typedef struct
    {
       int Hp;
       int Mp;
       int Exp;
    }PLAYER;
    
    
    void SaveGame(PLAYER PlayerStatsToSave, char* FileName)
    {
       FILE* File;
       File = fopen(FileName, "wb");
    
       if(File != NULL)
       {
          fwrite(&PlayerStatsToSave, sizeof(PLAYER), 1, File);
          fclose(File);
       }
    }
    
    
    void LoadGame(PLAYER* PointerToPlayerStats, char* FileName)
    {
       FILE* File;
       File = fopen(FileName, "rb");
    
       if(File != NULL)
       {
          fread(PointerToPlayerStats, sizeof(PLAYER), 1, File);
          fclose(File);
       }
    }
    This code was posted by Magos in the C++ forum. I trust it is what you were asking( btw it works the same way for classes).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  3. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM