Is there a way to fix the health number being display from both my player and boss class

When I run the code, everything sort of works besides the health system.

For example, boss_health = 150 and player_health = 100

The Boss weapon does 26 damage, and the player Sword weapon does 30 damage. But when the player attacks it does nothing. The Boss weapon does damage to itself and player, so the output for the boss health is now 124 and player health is 74… So, what am i doing wrong? why isn’t the player weapon being returned?

In my main.cpp the user can choose a weapon to equip to the player

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>void characterClass(){
Player player;
int response;
char changeInput;
Staff* staff = new Staff();
Book_Spells* book_spells = new Book_Spells();
Sword* sword = new Sword();
Shield* shield = new Shield();
Menu staff_menu(staff);
Menu book_menu(book_spells);
Menu sword_menu(sword);
Menu shield_menu(shield);
Equip* chosenWeapon = nullptr;
do{
LOG("--------------------------------------------")
LOG("| Choose Your Class |")
LOG("| 1-Staff 2-Book 3-Sword 4-Shield |")
LOG("--------------------------------------------")
std::cin >> response;
switch(response){
case 1:
cout << "Staff (DPS:" << staff_menu.item_bonus() << " DEF:" << staff_menu.item_defense() << ")" << endl;
chosenWeapon = staff;
break;
case 2:
cout << "Book of Spells (DPS:" << book_menu.item_bonus() << " DEF:" << book_menu.item_defense() << ")" << endl;
chosenWeapon = book_spells;
break;
case 3:
cout << "Sword (DPS:" << sword_menu.item_bonus() << " DEF:" << sword_menu.item_defense() << ")" << endl;
chosenWeapon = sword;
break;
case 4:
cout << "Shield (DPS:" << shield_menu.item_bonus() << " DEF:" << shield_menu.item_defense() << ")" << endl;
chosenWeapon = shield;
break;
case 5:
default:
LOG("Invalid Input")
break;
}
LOG("Do you want to pick a differnt class?(Y/N)")
std::cin >> changeInput;
}while(changeInput == 'Y' || changeInput == 'y');
//equips weapon to player in class
player.equip(chosenWeapon);
</code>
<code>void characterClass(){ Player player; int response; char changeInput; Staff* staff = new Staff(); Book_Spells* book_spells = new Book_Spells(); Sword* sword = new Sword(); Shield* shield = new Shield(); Menu staff_menu(staff); Menu book_menu(book_spells); Menu sword_menu(sword); Menu shield_menu(shield); Equip* chosenWeapon = nullptr; do{ LOG("--------------------------------------------") LOG("| Choose Your Class |") LOG("| 1-Staff 2-Book 3-Sword 4-Shield |") LOG("--------------------------------------------") std::cin >> response; switch(response){ case 1: cout << "Staff (DPS:" << staff_menu.item_bonus() << " DEF:" << staff_menu.item_defense() << ")" << endl; chosenWeapon = staff; break; case 2: cout << "Book of Spells (DPS:" << book_menu.item_bonus() << " DEF:" << book_menu.item_defense() << ")" << endl; chosenWeapon = book_spells; break; case 3: cout << "Sword (DPS:" << sword_menu.item_bonus() << " DEF:" << sword_menu.item_defense() << ")" << endl; chosenWeapon = sword; break; case 4: cout << "Shield (DPS:" << shield_menu.item_bonus() << " DEF:" << shield_menu.item_defense() << ")" << endl; chosenWeapon = shield; break; case 5: default: LOG("Invalid Input") break; } LOG("Do you want to pick a differnt class?(Y/N)") std::cin >> changeInput; }while(changeInput == 'Y' || changeInput == 'y'); //equips weapon to player in class player.equip(chosenWeapon); </code>
void characterClass(){
    Player player;

    int response;
    char changeInput;

    Staff* staff = new Staff();
    Book_Spells* book_spells = new Book_Spells();
    Sword* sword = new Sword();    
    Shield* shield = new Shield();

    Menu staff_menu(staff);
    Menu book_menu(book_spells);
    Menu sword_menu(sword);
    Menu shield_menu(shield);

    Equip* chosenWeapon = nullptr;

    do{
    LOG("--------------------------------------------")
    LOG("|            Choose Your Class             |")
    LOG("|    1-Staff  2-Book  3-Sword  4-Shield    |")
    LOG("--------------------------------------------")

    std::cin >> response;
    
    switch(response){
      case 1:
    cout << "Staff (DPS:" << staff_menu.item_bonus() << " DEF:" << staff_menu.item_defense() << ")" << endl;
    chosenWeapon = staff;
    break;
      case 2:
        cout << "Book of Spells (DPS:" << book_menu.item_bonus() << " DEF:" << book_menu.item_defense() << ")" << endl;
    chosenWeapon = book_spells; 
    break; 
      case 3:
    cout << "Sword (DPS:" << sword_menu.item_bonus() << " DEF:" << sword_menu.item_defense() << ")" << endl;
    chosenWeapon = sword;
    break;
      case 4:
    cout << "Shield (DPS:" << shield_menu.item_bonus() << " DEF:" << shield_menu.item_defense() << ")" << endl;
    chosenWeapon = shield;
    break;
      case 5:
      default:
        LOG("Invalid Input")
    break;
      }
     LOG("Do you want to pick a differnt class?(Y/N)")
     std::cin >> changeInput;
    }while(changeInput == 'Y' || changeInput == 'y');
    

    //equips weapon to player in class 
    player.equip(chosenWeapon);

Character.hpp

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>class Character {
private:
int atk;
int def;
int hp;
public:
virtual void equip(Equip* equipment) = 0;
virtual void attack(Character* target) {};
virtual void special() = 0;
void set_attack(int new_atk){ atk = new_atk; }
int get_attack() { return atk; }
void set_defense(int new_def){ def = new_def; }
int get_defense(){ return def; }
void set_hp(int new_hp){ hp = new_hp; }
int get_hp() { return hp; }
};
class Player : public Character{
private:
Equip* currentEquipment;
public:
void equip(Equip* equipment) override{
currentEquipment = equipment;
set_attack(currentEquipment->get_attack_bonus());
set_defense(currentEquipment->get_defense_bonus());
}
void attack(Character* target) override{
bool enemy; // logic to determine if target is enemy
int updateHealth;
if(enemy){
updateHealth = target->get_hp() - target->get_attack();
// apply damage to target
target->set_hp(updateHealth);
}
}
void special() override {
std::cout << "Defualt Implementationn";
}
};
class Boss : public Character{
private:
Equip* currentEquipment;
public:
void equip(Equip* equipment) override{
currentEquipment = equipment;
set_attack(currentEquipment->get_attack_bonus());
set_defense(currentEquipment->get_defense_bonus());
}
//overloading function
// equip 'sythe' weapon to boss
void equip(){
Equip* sythe = new Sythe();
equip(sythe);
delete sythe;
}
void attack(Character* target) override{
bool enemy;
int updateHealth;
equip();
if(enemy){
updateHealth = target->get_hp() - get_attack();
target->set_hp(updateHealth);
}
}
void special() override{
//special attacks go here
std::cout << "Defualt Implementationn";
}
};
</code>
<code>class Character { private: int atk; int def; int hp; public: virtual void equip(Equip* equipment) = 0; virtual void attack(Character* target) {}; virtual void special() = 0; void set_attack(int new_atk){ atk = new_atk; } int get_attack() { return atk; } void set_defense(int new_def){ def = new_def; } int get_defense(){ return def; } void set_hp(int new_hp){ hp = new_hp; } int get_hp() { return hp; } }; class Player : public Character{ private: Equip* currentEquipment; public: void equip(Equip* equipment) override{ currentEquipment = equipment; set_attack(currentEquipment->get_attack_bonus()); set_defense(currentEquipment->get_defense_bonus()); } void attack(Character* target) override{ bool enemy; // logic to determine if target is enemy int updateHealth; if(enemy){ updateHealth = target->get_hp() - target->get_attack(); // apply damage to target target->set_hp(updateHealth); } } void special() override { std::cout << "Defualt Implementationn"; } }; class Boss : public Character{ private: Equip* currentEquipment; public: void equip(Equip* equipment) override{ currentEquipment = equipment; set_attack(currentEquipment->get_attack_bonus()); set_defense(currentEquipment->get_defense_bonus()); } //overloading function // equip 'sythe' weapon to boss void equip(){ Equip* sythe = new Sythe(); equip(sythe); delete sythe; } void attack(Character* target) override{ bool enemy; int updateHealth; equip(); if(enemy){ updateHealth = target->get_hp() - get_attack(); target->set_hp(updateHealth); } } void special() override{ //special attacks go here std::cout << "Defualt Implementationn"; } }; </code>
class Character {
private:
    int atk;
    int def;
    int hp;

public:

    virtual void equip(Equip* equipment) = 0;
    virtual void attack(Character* target) {};
    virtual void special() = 0;

    void set_attack(int new_atk){ atk = new_atk; } 
    int get_attack() { return atk; }

    void set_defense(int new_def){ def = new_def; } 
    int get_defense(){ return def; }

    void set_hp(int new_hp){ hp = new_hp; }
    int get_hp() { return hp; }

};



class Player : public Character{

private:
    Equip* currentEquipment;

public: 

    void equip(Equip* equipment) override{
    currentEquipment = equipment;
    set_attack(currentEquipment->get_attack_bonus());
        set_defense(currentEquipment->get_defense_bonus());

    }

    void attack(Character* target) override{    
    bool enemy;  // logic to determine if target is enemy
    int updateHealth;

    if(enemy){
       updateHealth = target->get_hp() - target->get_attack();
       // apply damage to target
       target->set_hp(updateHealth);
    }

    }

    void special() override {
    std::cout << "Defualt Implementationn";
    }

};



class Boss : public Character{

private:
     Equip* currentEquipment;

public:

    void equip(Equip* equipment) override{
    currentEquipment = equipment;
    set_attack(currentEquipment->get_attack_bonus());
    set_defense(currentEquipment->get_defense_bonus()); 
   
   }
    //overloading function
    // equip 'sythe' weapon to boss
    void equip(){
    Equip* sythe = new Sythe();
    equip(sythe);

    delete sythe;
    }
   
    void attack(Character* target) override{
    bool enemy;
    int updateHealth;
        equip();

    if(enemy){
       updateHealth = target->get_hp() - get_attack();
       target->set_hp(updateHealth);
    }
    }

    void special() override{
        //special attacks go here
    std::cout << "Defualt Implementationn";
    }

};

game.cpp

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>constexpr int PLAYER_HEALTH = 100;
constexpr int BOSS_HEALTH = 200;
void fight(){
// when enemy is found, player can fight or run away...
Player player;
Boss boss;
// setting the health to player and enemy
player.set_hp(PLAYER_HEALTH);
boss.set_hp(BOSS_HEALTH);
string response;
int hit;
do{
boss.attack(&player);
player.attack(&boss);
cout << "Do you want to fight or run away?n";
std::cin >> response;
if(response == "fight"){
cout << "################" << endl;
cout << "Player Health: " << player.get_hp() << endl;
cout << "Boss Health: " << boss.get_hp() << endl;
cout << "################" << endl;
}
else if(response == "run"){
srand(time(NULL));
hit = rand() % 2 + 1;
if (hit == 1){
cout << "n-> Damage took when escaping: " << player.get_hp() << endl;
}
else{ cout << "n-> Took no damage when escaping." << endl; }
}
}while(player.get_hp() > 0 && player.get_hp() > 0 && response != "run");
if(player.get_hp() <= 0){ cout << "Game Over! You Died!" << endl; main();}
else if(boss.get_hp() <= 0){ cout << "Congrats! You Defeated the Boss!" << endl;}
}
</code>
<code>constexpr int PLAYER_HEALTH = 100; constexpr int BOSS_HEALTH = 200; void fight(){ // when enemy is found, player can fight or run away... Player player; Boss boss; // setting the health to player and enemy player.set_hp(PLAYER_HEALTH); boss.set_hp(BOSS_HEALTH); string response; int hit; do{ boss.attack(&player); player.attack(&boss); cout << "Do you want to fight or run away?n"; std::cin >> response; if(response == "fight"){ cout << "################" << endl; cout << "Player Health: " << player.get_hp() << endl; cout << "Boss Health: " << boss.get_hp() << endl; cout << "################" << endl; } else if(response == "run"){ srand(time(NULL)); hit = rand() % 2 + 1; if (hit == 1){ cout << "n-> Damage took when escaping: " << player.get_hp() << endl; } else{ cout << "n-> Took no damage when escaping." << endl; } } }while(player.get_hp() > 0 && player.get_hp() > 0 && response != "run"); if(player.get_hp() <= 0){ cout << "Game Over! You Died!" << endl; main();} else if(boss.get_hp() <= 0){ cout << "Congrats! You Defeated the Boss!" << endl;} } </code>
constexpr int PLAYER_HEALTH = 100;
constexpr int BOSS_HEALTH = 200;

void fight(){
    // when enemy is found, player can fight or run away...
    Player player;
    Boss boss;

    // setting the health to player and enemy
    player.set_hp(PLAYER_HEALTH);
    boss.set_hp(BOSS_HEALTH);
 
    
    string response;
    int hit;

    do{
    boss.attack(&player);
    player.attack(&boss);

    cout << "Do you want to fight or run away?n";
    std::cin >> response;

    if(response == "fight"){

    cout << "################" << endl; 
    cout << "Player Health: " << player.get_hp() << endl;
    cout << "Boss Health: " << boss.get_hp() << endl;
        cout << "################" << endl;

    }

    else if(response == "run"){ 

    srand(time(NULL));
    hit = rand() % 2 + 1;

    if (hit == 1){ 
       cout << "n-> Damage took when escaping: " << player.get_hp()  << endl;
        }
    
    else{ cout << "n-> Took no damage when escaping." << endl; }
      }

   }while(player.get_hp() > 0 && player.get_hp() > 0 && response != "run");


    if(player.get_hp() <= 0){ cout << "Game Over! You Died!" << endl; main();}

    else if(boss.get_hp() <= 0){ cout << "Congrats! You Defeated the Boss!" << endl;}

}


This is my first post, I hope i did it correctly.

I tried setting the health in private within each class, and i even tried setting the boss weapon equip in the main.cpp.

New contributor

Alejandro Barragan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật