Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6921973
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:20:45+00:00 2026-05-27T10:20:45+00:00

I am working on a final project for class and am having a little

  • 0

I am working on a final project for class and am having a little trouble. When I create my hero or monster object and assign it values, which is what i was checking with the commented out section in main, I am unable to access that info. For instance in my function to fight where I call the monster.Attack() the name of the warrior or mage object comes up blank. If I run a quick cout everything looks good except for the Hero objects members like name, type, maxHealth, maxMana, but the weapon name etc shows up. Any help would be greatly appreciated!

#include <iostream>
#include <vector>
#include <string>
#include <time.h>
#include <windows.h>

using namespace std;

struct Weapon;
struct Belt;
struct Potion;
struct Ability;
class Hero;
class Monster;
void CreateCharacters(Hero&, Hero&, Monster&);
int ShowMenu();
void Fight(Hero&, Hero&, Monster&);



struct Weapon{
    std::string name;
    int damage;
};

struct Belt{
    std::vector<Potion> contents;
};

struct Potion{
    enum e_Type{Health, Mana} type;

    int amount;
};

struct Ability{
    std::string name;

    enum e_Type{Offensive, Defensive, Neutral} type;    

    int manaCost;
    int amount;
};

class Hero{

public:
    std::string name;   
    int maxHealth;  
    int maxMana;        

public: 
    Weapon weapon;
    Belt belt;
    std::vector<Ability> abilities;
    enum e_HeroType{Warrior, Mage} heroType;
    int health;
    int mana;

    void CreateHero(std::string name, int maxHealth, int maxMana, Hero::e_HeroType heroType){
            name = name;
            maxHealth = maxHealth;
            maxMana = maxMana;
            health = maxHealth;
            mana = maxMana;
            heroType = heroType;
    }
    void CreateWeapon(std::string name, int damage){
        weapon.name = name;
        weapon.damage = damage;
    }
    void FillBelt(Potion::e_Type type, int amount){
        Potion potion;
        potion.type = type;
        potion.amount = amount;
        for (int i = 0; i < 3; i++){
            belt.contents.push_back(potion);
        }
    }
    void CreateAbility(std::string name, Ability::e_Type type, int manaCost, int amount){
        Ability ability;
        ability.name = name;
        ability.type = type;
        ability.manaCost = manaCost;
        ability.amount = amount;
        abilities.push_back(ability);
    }
    void Attack(){}
    void TakeDamage(){}
    void UseItem(){}

};

class Monster{

private:
    std::string name;   
    int maxHealth;

public: 
    int health;
    Weapon weapon;

    void CreateMonster(std::string name, int maxHealth){
            name = name;
            maxHealth = maxHealth;
            health = maxHealth;
    }
    void CreateWeapon(std::string name, int damage){
        weapon.name = name;
        weapon.damage = damage;
    }
    void Attack(Hero &mage, Hero &warrior, Monster &monster){

        std::cout << "The troll takes a random swing!" << endl;
        Sleep(3000);


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

        if (randomNumber == 1){
            mage.health -= monster.weapon.damage;
            cout << mage.name << " has been struck!" << endl;
            Sleep(2500);
            cout << "The health of " << mage.name << " has dropped to " << mage.health << endl;
        } else if (randomNumber == 2){
            warrior.health -= monster.weapon.damage;
            cout << warrior.name << " has been struck!" << endl;
            Sleep(2500);
            cout << "The health of " << warrior.name << " has dropped to " << warrior.health << endl;
        }
    }
    void TakeDamage(){}
};

void CreateCharacters(Hero &mage, Hero &warrior, Monster &monster){

    mage.CreateHero("Gandalf", 80, 100, Hero::Mage);
    mage.CreateWeapon("Rod of Ages", 5);
    mage.FillBelt(Potion::Mana, 25);
    mage.CreateAbility("Fireball", Ability::Offensive, 15, 25);
    mage.CreateAbility("Heal", Ability::Defensive, 10, 25);
    warrior.CreateHero("Spartacus", 100, 0, Hero::Warrior);
    warrior.CreateWeapon("The Destroyer", 35);
    warrior.FillBelt(Potion::Health, 25);
    monster.CreateMonster("Trollio", 250);
    monster.CreateWeapon("Mighty Club", 15);
}

int ShowMenu(){

    int choice;

    cout << "\nWelcome to the Battle!" << endl;
    cout << "----------------------" << endl;
    cout << "Please make a choice from the following:" << endl;
    cout << "1. Fight Now!" << endl;
    cout << "2. Quit" << endl;

    cin >> choice;

    return choice;
}

void Fight(Hero &mage, Hero &warrior, Monster &monster){

    std::cout << "\nYou have chosen to fight!" << endl;
    Sleep(3000);
    monster.Attack(mage, warrior, monster);

}

int main(){

    Hero mage;
    Hero warrior;
    Monster monster;

    CreateCharacters(mage, warrior, monster);

    do {
        int choice = ShowMenu();

        if (choice == 1){

            Fight(mage, warrior, monster);

        } else if (choice == 2){

            break;

        }
    } while (true); 

    cin.ignore();
    cin.get();

    return 0;
}

Whats also confusing to me is that the monster.Attack() function properly displays the health decremented for the mage or warrior but wont display on a cout.

Here is the output of the little cout test I did, notice the top 4 and about halfway through the other four referencing the objects herotype, name, maxHealth and maxMana dont register.

-858993460

-858993460
-858993460
5
Rod of Ages
Fireball
0
25
15
Heal
1
25
10
1
25
1
25
1
25
-858993460

-858993460
-858993460
35
The Destroyer
0
25
0
25
0
25
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T10:20:46+00:00Added an answer on May 27, 2026 at 10:20 am

    I think the problem is the argument names are the same as your member variable names:

    void CreateHero(std::string name, int maxHealth, int maxMana, Hero::e_HeroType heroType){
            name = name; // Assigning member variable to itself.
            ...
    

    Try changing to:

    void CreateHero(std::string a_name, int a_maxHealth, int a_maxMana, Hero::e_HeroType a_heroType){
            name = a_name;
            ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been working on my final programming class project, and I am stuck right
I am working on final project for my C# class. My project is a
I'm working on a project which is saving some images to sdcard and now
I'm working on my final year project using java swings and jasper reports for
I'm working on a project using Java RMI. This is the class causing problem:
I am working on a little pinball-game project for a hobby and am looking
Please help with my final project in my intro/intermediate programming class. Using stored procedures,table
I'm currently working on a Java EE project which uses JPA. I'm connecting to
I'm working on a little game/project and I decide to use AndEngine for it
I am working on an Android project in which I use a ViewFlipper for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.