Hey all,

I am currently working on building a small text-based RPG to practice C++. I am working on a function to load a saved game from information saved in a text file. Below is the code for the function:

void getSavedGame(playableCharacter& player1)
{
	int stamina, health, mana, level, strength, constitution, dexterity, attack, strReq, dexReq, defense, strReq2;
	bool newGame;
	string weaponName, armorName;
	ifstream savedGame;
	savedGame.open("savedgame.txt");
		if(savedGame.is_open())
		{
			savedGame >> stamina;
			savedGame >> health;
			savedGame >> mana;
			savedGame >> level;
			savedGame >> strength;
			savedGame >> constitution;
			savedGame >> dexterity;
			savedGame >> newGame;
			getline(savedGame, weaponName);
			savedGame >> attack;
			savedGame >> strReq;
			savedGame >> dexReq;
			getline(savedGame, armorName);
			savedGame >> defense;
			savedGame >> strReq2;
			player1.setStamina(stamina);
			player1.setHealth(health);
			player1.setMana(mana);
			player1.setLevel(level);
			player1.setStrength(strength);
			player1.setConstitution(constitution);
			player1.setDexterity(dexterity);
			player1.setNewGame(newGame);
			player1.pMainWeapon->setName(weaponName);
			player1.pMainWeapon->setAttack(attack);
			player1.pMainWeapon->setStrReq(strReq);
			player1.pMainWeapon->setDexReq(dexReq);
			player1.pMainArmor->setName(armorName);
			player1.pMainArmor->setDefense(defense);
			player1.pMainArmor->setStrReq(strReq2);
		}
		else
		{
			cout << "The saved game did not load successfully" << endl;
		}
	savedGame.close();
}

Everything is working fine until it comes time for the code to read in the weaponName string variable. The program is compiling correctly and the code worked fine before I added in the reading of the string variables. The text file that it is reading from is a .txt that is formatted with one variable per line. When the program runs, it get's to reading that variable and then throws the "This program has encountered an error and must close" from Windows XP.

Does anybody have any idea why I would be having this issue?

Don't use the getline methods, just redirect from the stream as you would for any of the other variables (e.g, savedGame >> weaponName; and same for armorName). If you are writing the files manually it's finicky about the booleans (requiring 0 or 1 representation, I think I didn't explore it further). AFAIK, you can chain all those inputs into one statement with savedGame >> var1 >> var2 etc etc.

@jonsca - I tried using the same method as the the other variables (e.g. savedGame >> weaponName) but I ended up with the same issue.

@Ancient Dragon - below is the .txt file that I am using. I hope that it helps.

-D

Attachments
25
25
10
2
13
12
11
0
Iron Short Sword
3
2
0
Soft Leather Armor
2
2

Also, as a note, I am using 0 and 1 for my boolean value (as you can see from the attached file) and I am also using Visual C++ 2008 Express.

-D

The problem is that you are leaving the '\n' in the input buffer before calling getline(). call ignore() just before getline()

if(savedGame.is_open())
		{
			savedGame >> stamina;
			savedGame >> health;
			savedGame >> mana;
			savedGame >> level;
			savedGame >> strength;
			savedGame >> constitution;
			savedGame >> dexterity;
			savedGame >> newGame;
            savedGame.ignore(1000,'\n');
			getline(savedGame, weaponName);
			savedGame >> attack;
			savedGame >> strReq;
			savedGame >> dexReq;
            savedGame.ignore(1000,'\n');
			getline(savedGame, armorName);
			savedGame >> defense;
			savedGame >> strReq2;

Edited 7 Years Ago by Ancient Dragon: n/a

I had tried Ancient's suggestion and wasn't able to get it to work (but it's a sound idea so it's more than probable that's on me) but when I took your example .txt file and removed the spaces from the strings (you could replace them with underscores which would be easy to strip for use in the program) it works with savedGame >> armorName .
Here's my output from your program.

25
25
10
2
13
12
11
0
IronShortSword
3
2
0
SoftLeatherArmor
2
2

Thank you, guys. I was able to get the variable to load what I wanted using Ancient Dragon's method, only slightly modified.

Unfortunately that is not the issue that was causing the program to crash. I have found out that it has to do with the line:

player1.pMainWeapon->setName(weaponName);

Everything is running correctly up to there. When I debug the code, i am getting an error saying that the program can not access that memory. I don't expect you guys to be able to decipher the problem just from that and it is a lot of code to put into a post that already seems to be dying out.

I will try to work it out tonight, and if I can't seem to get it working then I will start a new thread with the additional code. Thank you again for your help.

-D

It does pretty much exactly what it says. It sets the name of the weapon object that is currently in use. Here is a snippet of the code:

class weapon{
private:
   string weaponName;
public:
   void setName(string name);
};

void weapon::setName(string name)
{
     weaponName = name;
}

Then I have my playable character class that has a pointer to a weapon and the constructor creates a weapon object and sets the pointer equal to it:

class playableCharacter{
public:
   weapon* pMainWeapon;
   weapon();
};

weapon::weapon()
{
     weapon mainWeapon;
     pMainWeapon = &mainWeapon;
}

It is from there that I attempt to get the name from a txt file and use the setName() function to set the name of the weapon.

Please note that this is not the entire code, just what seems to be applicable to this scenario.

-D

Edited 7 Years Ago by dgr231: n/a

I wrote the second snippet of code above incorrectly. It is supposed to read as follows:

class playableCharacter{
public:
   weapon* pMainWeapon;
   playableCharacter();
};

playableCharacter::playableCharacter()
{
     weapon mainWeapon;
     pMainWeapon = &mainWeapon;
}

Sorry for any confusion that may have caused...

-D

Edited 7 Years Ago by dgr231: n/a

This question has already been answered. Start a new discussion instead.