Please support our C++ advertiser: Programming Forums
Views: 3451 | Replies: 21
![]() |
•
•
Join Date: Jul 2005
Posts: 37
Reputation:
Rep Power: 4
Solved Threads: 0
ok I'm trying to save all of these variables:
#include <fstream>
#include <cstdlib>
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <string>
#include <conio.h>
using namespace std;
int trainingHut(void);
int forest(void);
void menuFunc(void);
int weaponShop(void);
int forbidenPalece(void);
int field(void);
int rest(void);
int beach(void);
int armorShop(void);
int attackPhase(void);
int deffensePhase(void);
int itemfind(void);
int attackAsk(void);
//inventory
string currentWeapon;
string currentAcessory;
string currentArmor;
string currentShield;
string currentFootwear;
string usedWeapons[200];
int index = 0;
//stats
int hp = 50;
int xp = 0;
int atk = 17;
int lvl = 1;
int def = 14;
int armdef = 0;
int wepatk = 0;
int mana = 15;
int acc = 11;
int eva = 9;
int spllvl = 1;
int totAtk = atk + wepatk;
int totDef = def + armdef;
//monster stuff
string monster;
int monsterHp;
int monsterAtk;
int monsterDef;
int monsterAcc;
int monsterEva;
int damage;
int monsterDamage;
int temporaryAtk = totAtk;
int temporaryDef = totDef;
//all weapons
string maces = "maces";
string clubs = "clubs";
string swords ="swords";
string scimitars = "scimitars";
string axes = "axe";
string daggers = "daggers";
//all defenses
string shield;
string armor;
string helmet;
//all other
string rings;
string footwear;
string gloves;
string necklaces;
string cape;
//all words
string yes;
string Hi;
int menu;
int gold = 500;
//maces
string spikedmaces = "spicked";
string bronzemaces = "bronze";
string ironmaces = "iron";
string silvermaces = "silver";
string icemaces = "ice";
string firemaces = "fire";
string goldmaces = "gold";
string dimondmaces = "dimond";
//armor
string spikedarmor = "spicked";
string bronzearmor = "bronze";
string ironarmor = "iron";
string silverarmor = "silver";
string icearmor = "ice";
string firearmor = "fire";
string goldarmor = "gold";
string dimondarmor = "dimond";
:to a file during the program and then when i start up my program again i want it to cheak and see if the variables are saved to a file like "saveOne" and if so i want the variables that were saved there replace the new ones if not, continue regularly
i saw the tutorial but it just told me how to make it write something not write a variable and then load it again
#include <fstream>
#include <cstdlib>
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <string>
#include <conio.h>
using namespace std;
int trainingHut(void);
int forest(void);
void menuFunc(void);
int weaponShop(void);
int forbidenPalece(void);
int field(void);
int rest(void);
int beach(void);
int armorShop(void);
int attackPhase(void);
int deffensePhase(void);
int itemfind(void);
int attackAsk(void);
//inventory
string currentWeapon;
string currentAcessory;
string currentArmor;
string currentShield;
string currentFootwear;
string usedWeapons[200];
int index = 0;
//stats
int hp = 50;
int xp = 0;
int atk = 17;
int lvl = 1;
int def = 14;
int armdef = 0;
int wepatk = 0;
int mana = 15;
int acc = 11;
int eva = 9;
int spllvl = 1;
int totAtk = atk + wepatk;
int totDef = def + armdef;
//monster stuff
string monster;
int monsterHp;
int monsterAtk;
int monsterDef;
int monsterAcc;
int monsterEva;
int damage;
int monsterDamage;
int temporaryAtk = totAtk;
int temporaryDef = totDef;
//all weapons
string maces = "maces";
string clubs = "clubs";
string swords ="swords";
string scimitars = "scimitars";
string axes = "axe";
string daggers = "daggers";
//all defenses
string shield;
string armor;
string helmet;
//all other
string rings;
string footwear;
string gloves;
string necklaces;
string cape;
//all words
string yes;
string Hi;
int menu;
int gold = 500;
//maces
string spikedmaces = "spicked";
string bronzemaces = "bronze";
string ironmaces = "iron";
string silvermaces = "silver";
string icemaces = "ice";
string firemaces = "fire";
string goldmaces = "gold";
string dimondmaces = "dimond";
//armor
string spikedarmor = "spicked";
string bronzearmor = "bronze";
string ironarmor = "iron";
string silverarmor = "silver";
string icearmor = "ice";
string firearmor = "fire";
string goldarmor = "gold";
string dimondarmor = "dimond";
:to a file during the program and then when i start up my program again i want it to cheak and see if the variables are saved to a file like "saveOne" and if so i want the variables that were saved there replace the new ones if not, continue regularly
i saw the tutorial but it just told me how to make it write something not write a variable and then load it again
•
•
Join Date: Jul 2005
Posts: 244
Reputation:
Rep Power: 4
Solved Threads: 4
Let me just assume that you've got an inventory that only lets you have one particular set of equipment at a time.
If that is the case, why bother writing variables when you could write a character string that contains the current information?
Here's what I mean:
Let's take your example, and make it into a single string for maces and armor types.
Okay. Those comments are the character representations for those items.
Now:
Suppose you're blinged out with a diamond mace and gold armor. When you want to save, you do the following:
And you save the equip array.
When you load the game, you then parse the input information. Based on the character contents of the string, you can equip yourself without storing huge numbers of variables.
If that is the case, why bother writing variables when you could write a character string that contains the current information?
Here's what I mean:
Let's take your example, and make it into a single string for maces and armor types.
//maces string spikedmaces = "spiked"; // s string bronzemaces = "bronze";// b string ironmaces = "iron"; //i string silvermaces = "silver"; //a string icemaces = "ice";// c string firemaces = "fire";//f string goldmaces = "gold";//g string diamondmaces = "diamond";//d //armor string spikedarmor = "spiked";//s string bronzearmor = "bronze";//b string ironarmor = "iron"; //i string silverarmor = "silver"; //a string icearmor = "ice"; //c string firearmor = "fire";//f string goldarmor = "gold";//g string dimondarmor = "diamond";//d
Okay. Those comments are the character representations for those items.
Now:
char equip[2]; //Character equipment array. 0 is weapon; 1 is armor
Suppose you're blinged out with a diamond mace and gold armor. When you want to save, you do the following:
equip[0] = 'd'; //thus making your weapon a diamond mace equip[1] = 'g';//this is your golden armor
When you load the game, you then parse the input information. Based on the character contents of the string, you can equip yourself without storing huge numbers of variables.
•
•
Join Date: Jul 2005
Posts: 244
Reputation:
Rep Power: 4
Solved Threads: 4
It's called a struct or class. But if you don't mind me saying, your implementation, while straightforward, isn't very efficient.
You're going to have to write this stuff as structures, then have it saved in binary, using ofstream and write. It's got its appeals...
Here's a quick example:
You're going to have to write this stuff as structures, then have it saved in binary, using ofstream and write. It's got its appeals...
Here's a quick example:
#include<iostream>
#include<fstream>
#include<cstring>
using namespace std;
struct example{
char name[40];
int potions;
};
int main()
{
struct example hero; //declares instance of struct type 'example' called 'hero'.
strcpy(hero.name, "Leroy Jheeenkins");
hero.potions = 5;
//write data
ofstream save_1("savefile", ios::out|ios::binary);
if(!savefile) //Couldn't open save file.
{
cout<< "Save failed."<<endl;
return 1;
}
save_1.write((char *) &hero, sizeof(struct example)); //writes entire struct to file.
save_1.close(); //closes file stream
return 1;
}•
•
Join Date: Jul 2005
Posts: 244
Reputation:
Rep Power: 4
Solved Threads: 4
See my last post for an edit. To get the info out of the file, you have to use ifstream and read.
Edit: This lets you use the hero struct and all its information until the program exits.
That's really the best way to do it simply, but I still think your method is rather awkward and uses a lot of resources it doesn't have to.
ifstream load_1("savefile", ios::in | ios::binary);
if(!load_1)
{
cout<< "Cannot load file"<<endl;
return 1;
}
load_1.read((char *) &hero, sizeof(struct example)); //loads the saved struct.
cout<<hero.name<<endl;
cout<<"Number of potions: " << hero.potions<<endl;
load_1.close();
return 0;
}
That's really the best way to do it simply, but I still think your method is rather awkward and uses a lot of resources it doesn't have to.
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)





Linear Mode