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 -_-