# Struct Method for Loops Problem [closed]

I have tried numerous times how to make a do-while loop using the float constructor for my code but it seems it does not work properly as I wanted. For summary, I am making a TBRPG in C++ and I encountered few problems. But before that, let me post my code.

#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>

using namespace std;
int char_level = 1; //the starting level of the character.
string town; //town
string town_name; //the name of the town the character is in.
string charname; //holds the character's name upon the start of the game
int gems = 0; //holds the value of the games the character has.
const int MAX_ITEMS = 15; //max items the character can carry
string inventory [MAX_ITEMS]; //the inventory of the character in game
int itemnum = 0; //number of items that the character has.
bool GameOver = false; //boolean intended for the game over scr.

string monsterTroop [] = {"Slime", "Zombie", "Imp", "Sahaguin, Hounds, Vampire"}; //monster name
float monsterTroopHealth [] = {5.0f, 10.0f, 15.0f, 20.0f, 25.0f}; // the health of the monsters
int monLifeBox; //life carrier of the game's enemy troops
int enemNumber; //enemy number

//inventory[itemnum++] = "Sword";

class RPG_Game_Enemy {
public:
void enemyAppear ()
{
srand(time(0));
enemNumber = 1+(rand()%3);
if (enemNumber == 1)
cout << monsterTroop[1]; //monster troop 1
else if (enemNumber == 2)
cout << monsterTroop[2]; //monster troop 2
else if (enemNumber == 3)
cout << monsterTroop[3]; //monster troop 3
else if (enemNumber == 4)
cout << monsterTroop[4]; //monster troop 4

}

void enemDefeat () {
cout << "The foe has been defeated. You are victorious." << endl;
}

void enemyDies()
{ //if the enemy dies:
//collapse declaration
cout << "The foe vanished and you are victorious!" << endl;
}
};

class RPG_Scene_Battle {
public:
RPG_Scene_Battle(float ini_health) : health (ini_health){};
float getHealth()
{
return health;

}
void setHealth(float rpg_val){ health = rpg_val;};

private:
float health;
};

//---------------------------------------------------------------//
// Conduct Damage for the Scene Battle's Damage
//---------------------------------------------------------------//

float conductDamage(RPG_Scene_Battle rpg_tr, float damage) {
rpg_tr.setHealth(rpg_tr.getHealth() - damage);
return rpg_tr.getHealth();
};

// ------------------------------------------------------------- //

void RPG_Scene_DisplayItem ()
{
cout << "Items: \n";
for (int i=0; i < itemnum; ++i)
cout << inventory[i] <<endl;
};


In this code I have so far, the problem I have is the battle scene. For example, the player battles a Ghost with 10 HP, when I use a do while loop to subtract the HP of the character and the enemy, it only deducts once in the do while. Some people said I should use a struct, but I have no idea how to make it. Is there a way someone can display a code how to implement it on my game?

Edit:

I made the do-while by far like this:

do
RPG_Scene_Battle (player, 20.0f);
RPG_Scene_Battle (enemy, 10.0f);
cout << "Battle starts!" <<endl;
cout << "You used a blade skill and deducted 2 hit points to the enemy!"
conductDamage (enemy, 2.0f);
while (enemy!=0)


also, I made something like this:

#include <iostream>
using namespace std;

int gems = 0;

class Entity {
public:
Entity(float startingHealth) : health(startingHealth){}; // initialize health
float getHealth(){return health;}
void setHealth(float value){ health = value;};
private:
float health;
};

float subtractHealthFrom(Entity& ent, float damage) {
ent.setHealth(ent.getHealth() - damage);
return ent.getHealth();
};

int main () {

Entity character(10.0f);
Entity enemy(10.0f);

cout << "Hero Life: ";
cout << subtractHealthFrom(character, 2.0f) <<endl;
cout << "Monster Life: ";
cout << subtractHealthFrom(enemy, 2.0f) <<endl;
cout << "Hero Life: ";
cout << subtractHealthFrom(character, 2.0f) <<endl;
cout << "Monster Life: ";
cout << subtractHealthFrom(enemy, 2.0f) <<endl;


};

Struct method, they say, should solve this problem. How can I continously deduct hp from the enemy? Whenever I deduct something, it would return to its original value -_-

## closed as too localized by MichaelHouse♦, doppelgreener, jcora, Josh♦, michael.bartnettOct 9 '12 at 3:15

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. If this question can be reworded to fit the rules in the help center, please edit the question.

• Most of the code you posted doesn't appear to be relevant to the problem...and you haven't posted the actual do-while loop, which would be relevant. Can you edit your question to include that, and explain a bit more what your current code is doing wrong vs what you want it to be doing? – Nathan Reed Sep 18 '12 at 23:39
• cplusplus.com/doc/tutorial/structures – Cypher Sep 19 '12 at 0:03

There are two reasons the damage resets each time. First of all, you declared your RPG_Scene_Battle variables inside the loop, which means they get destroyed and recreated each time around the loop. You probably wanted something like:

RPG_Scene_Battle player(20.0f);
RPG_Scene_Battle enemy(10.0f);
do {
cout << "Battle starts!" << endl;
cout << "You used a blade skill and deducted 2 hit points to the enemy!" << endl;
conductDamage(enemy, 2.0f);
} while (enemy.getHealth() > 0);


Here, the RPG_Scene_Battles are created once and the same variables are used each time around the loop, so changes will be retained.

Second, your conductDamage function is intended to modify the RPG_Scene_Battle that's passed in - but it doesn't, because that parameter is being passed by value; each call to conductDamage is effectively making a new copy of the RPG_Scene_Battle, modifying that copy, then throwing it away. To fix this, either pass the parameter by reference or (better yet) make conductDamage a method of RPG_Scene_Battle instead of a free function.

• thank you. this is by far the best answer I read. I will try this now and fix my code. gratitude to you, Nathan :) – Annalyne Sep 19 '12 at 0:18