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?