943,776 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 7072
  • C++ RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Jul 22nd, 2005
0

Re: help!! how do you save a variable to a...

aha! Thank you very much.
Reputation Points: 10
Solved Threads: 0
Light Poster
Rearden is offline Offline
26 posts
since Jul 2005
Jul 22nd, 2005
0

Re: help!! how do you save a variable to a...

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
Reputation Points: 16
Solved Threads: 0
Light Poster
jasondrey13 is offline Offline
41 posts
since Jul 2005
Jul 22nd, 2005
0

Re: help!! how do you save a variable to a...

Ohoho! I see.
There's an easy way to do this, but first, some more information.
For this game, do you have an inventory that allows you to hold multiple copies of armor, for example? Or can you only have one at a time?
Reputation Points: 22
Solved Threads: 5
Posting Whiz in Training
Drowzee is offline Offline
244 posts
since Jul 2005
Jul 22nd, 2005
0

Re: help!! how do you save a variable to a...

a.
invintory
Reputation Points: 16
Solved Threads: 0
Light Poster
jasondrey13 is offline Offline
41 posts
since Jul 2005
Jul 22nd, 2005
0

Re: help!! how do you save a variable to a...

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.
C++ Syntax (Toggle Plain Text)
  1. //maces
  2. string spikedmaces = "spiked"; // s
  3. string bronzemaces = "bronze";// b
  4. string ironmaces = "iron"; //i
  5. string silvermaces = "silver"; //a
  6. string icemaces = "ice";// c
  7. string firemaces = "fire";//f
  8. string goldmaces = "gold";//g
  9. string diamondmaces = "diamond";//d
  10.  
  11. //armor
  12. string spikedarmor = "spiked";//s
  13. string bronzearmor = "bronze";//b
  14. string ironarmor = "iron"; //i
  15. string silverarmor = "silver"; //a
  16. string icearmor = "ice"; //c
  17. string firearmor = "fire";//f
  18. string goldarmor = "gold";//g
  19. string dimondarmor = "diamond";//d

Okay. Those comments are the character representations for those items.
Now:

C++ Syntax (Toggle Plain Text)
  1. 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:

C++ Syntax (Toggle Plain Text)
  1. equip[0] = 'd'; //thus making your weapon a diamond mace
  2. equip[1] = 'g';//this is your golden armor
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.
Reputation Points: 22
Solved Threads: 5
Posting Whiz in Training
Drowzee is offline Offline
244 posts
since Jul 2005
Jul 22nd, 2005
0

Re: help!! how do you save a variable to a...

do you know a way that i could just do it variable by variable like if i was just going to save just one variable at a time?and then load it couldyou do like a whole example of like saving just a "money" variable?
Reputation Points: 16
Solved Threads: 0
Light Poster
jasondrey13 is offline Offline
41 posts
since Jul 2005
Jul 22nd, 2005
0

Re: help!! how do you save a variable to a...

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:

C++ Syntax (Toggle Plain Text)
  1. #include<iostream>
  2. #include<fstream>
  3. #include<cstring>
  4. using namespace std;
  5.  
  6. struct example{
  7. char name[40];
  8. int potions;
  9. };
  10.  
  11. int main()
  12. {
  13. struct example hero; //declares instance of struct type 'example' called 'hero'.
  14. strcpy(hero.name, "Leroy Jheeenkins");
  15. hero.potions = 5;
  16.  
  17.  
  18. //write data
  19.  
  20. ofstream save_1("savefile", ios::out|ios::binary);
  21. if(!savefile) //Couldn't open save file.
  22. {
  23. cout<< "Save failed."<<endl;
  24. return 1;
  25. }
  26. save_1.write((char *) &hero, sizeof(struct example)); //writes entire struct to file.
  27. save_1.close(); //closes file stream
  28. return 1;
  29. }
Reputation Points: 22
Solved Threads: 5
Posting Whiz in Training
Drowzee is offline Offline
244 posts
since Jul 2005
Jul 22nd, 2005
0

Re: help!! how do you save a variable to a...

do you know a way that i could just do it variable by variable like if i was just going to save just one variable at a time?and then load it couldyou do like a whole example of like saving just a "money" variable?
Reputation Points: 16
Solved Threads: 0
Light Poster
jasondrey13 is offline Offline
41 posts
since Jul 2005
Jul 22nd, 2005
0

Re: help!! how do you save a variable to a...

See my last post for an edit. To get the info out of the file, you have to use ifstream and read.

C++ Syntax (Toggle Plain Text)
  1. ifstream load_1("savefile", ios::in | ios::binary);
  2. if(!load_1)
  3. {
  4. cout<< "Cannot load file"<<endl;
  5.  
  6. return 1;
  7. }
  8. load_1.read((char *) &hero, sizeof(struct example)); //loads the saved struct.
  9. cout<<hero.name<<endl;
  10. cout<<"Number of potions: " << hero.potions<<endl;
  11. load_1.close();
  12. return 0;
  13. }
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.
Reputation Points: 22
Solved Threads: 5
Posting Whiz in Training
Drowzee is offline Offline
244 posts
since Jul 2005
Jul 22nd, 2005
0

Re: help!! how do you save a variable to a...

yuuuyuyuiuttyi never mind




thx
Reputation Points: 16
Solved Threads: 0
Light Poster
jasondrey13 is offline Offline
41 posts
since Jul 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: is dave sinkula a little boy?
Next Thread in C++ Forum Timeline: require a little help with first program with class's





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC