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 8537337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:52:42+00:00 2026-06-11T10:52:42+00:00

My friend and I are learning C++ and though we have been able to

  • 0

My friend and I are learning C++ and though we have been able to beat down every problem that has come our way (and have learned a bunch by doing so) in this program, this one has been making us rack our brains all day. We have spent countless hours not only trying to solve it ourselves but also looking through Stackoverflow’s related questions and doing tons of Google Searches… Finally we decided that we have to just ask and hope one of you has an idea of what our problem is.

Basically we are trying to create a text based (console only) RPG and we have gotten to the point where we are creating Player Statistics using a class defined in our one and only Header file, there are three constructors to the class (using function overloading of course) and although we are able to run the game error free it seems as though we are not able to actually edit the values of the class member variables in any way!

This is our class:

//File OverHeader.h

class PlayerStatistics
{
public:
    PlayerStatistics(int HitPoints, int MagickaPoints, int Fatigue, int Damage, int Defense, int Dodge, int Block, int SpellCastChance);
    PlayerStatistics(int Experience, int Level);
    PlayerStatistics();

    int HitPoints;
    int MagickaPoints;
    int Fatigue;
    int Damage;
    int Defense;

    // Chance Based System (Relies on Fatigue Level)
    int Dodge;
    int Block;
    int SpellCastChance;

    int Experience;
    int Level;
};

And the main function:

#include <iostream>
#include <string>
#include "OverHeader.h"

// MAIN FUNCTION DEFINITION
int main()
{
PlayerStatistics PlayerStats(20, 20, 20, 20, 20, 20, 20, 20);
PlayerStatistics PlayerStatsLevel(0, 1);

    //continued with code irrelevant to this question.
}

The trouble is that the lines in the Main Function don’t actually set the integer variables in the class to those values. After those two lines the integer values should be set to (just for demonstration purposes):

int HitPoints == 20;
int MagickaPoints == 20;
int Fatigue == 20;
int Damage == 20;
int Defense == 20;
int Dodge == 20;
int Block == 20;
int SpellCostChance == 20;
int Experience == 0;
int Level == 1;

But strangely enough outputting ANY of these integers will simply output random numbers (presumably the memory addresses current values).

The three constructors are in fact defined properly (although not in the Main.cpp) here:

// PlayerCreation.cpp
PlayerStatistics::PlayerStatistics(int HitPoints, int MagickaPoints, int Fatigue, int Damage, int Defense, int Dodge, int Block, int SpellCastChance)
{

}

PlayerStatistics::PlayerStatistics(int Experience, int Level)
{

}

PlayerStatistics::PlayerStatistics()
{

}

And that is our problem, I hope I described everything clearly enough, please let me know if you can help us actually edit the values of these Class Member Variables! All help is appreciated!

  • 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-06-11T10:52:44+00:00Added an answer on June 11, 2026 at 10:52 am

    This creates the same class in two different ways:

    PlayerStatistics PlayerStats(20, 20, 20, 20, 20, 20, 20, 20);
    PlayerStatistics PlayerStatsLevel(0, 1);
    

    The first creates a variable called PlayerStats that uses the first constructor. The second creates another variable called PlayerStatsLevel that uses the second constructor.

    Now, the constructors…

    // PlayerCreation.cpp
    PlayerStatistics::PlayerStatistics(int HitPoints, int MagickaPoints, int Fatigue, int Damage, int Defense, int Dodge, int Block, int SpellCastChance)
    {
    
    }
    
    PlayerStatistics::PlayerStatistics(int Experience, int Level)
    {
    
    }
    

    This doesn’t actually initialise the member variables in the class. You have used the same names in the constructor, but that is actually going to give you grief because (to take a single example) the local variable HitPoints passed into the constructor overrides the class member HitPoints. Now, if you want to refer to the class member you must use this->HitPoints.

    So, I reiterate, nothing actually got initialised. So you have random values in your object. You need to do this (I’ll take the shorter example):

    // Using initializer list
    PlayerStatistics::PlayerStatistics(int inExperience, int inLevel)
        : Experience(inExperience), Level(inLevel)
    {    
    }
    
    // Or using conventional assignment
    PlayerStatistics::PlayerStatistics(int inExperience, int inLevel)
    {    
        Experience = inExperience;
        Level = inLevel;
    }
    

    Note that the other values were not initialized using this constructor. That might be intended, or you might want to set them all to a default value. You need to do this explicitly.

    Now, it kinda looks like you wanted a single instance with all those stats set. What you might want to do is use just one constructor – the empty constructor PlayerStatistics(), and initialize everything to something ‘sensible’. Then define functions to set the stats in chunks:

    void PlayerStatistics::SetStats(int HitPoints, int MagickaPoints, int Fatigue, int Damage, int Defense, int Dodge, int Block, int SpellCastChance)
    {
        this->HitPoints = HitPoints;
        this->MagickaPoints = MagickaPoints;
        // etc etc...
    }
    
    void PlayerStatistics::SetLevel(int Experience, int Level)
    {
        this->Experience = Experience;
        this->Level = Level;
    }
    
    // If you want you can explicitly set the values to something in the constructor.
    // It's probably good practice if you're new at this.
    PlayerStatistics::PlayerStatistics()
    {    
        SetStats(0, 0, 0, 0, 0, 0, 0, 0);
        SetLevel(0, 0);
    }
    

    Now in your main:

    PlayerStatistics player;
    player.SetStats(20, 20, 20, 20, 20, 20, 20, 20);
    player.SetLevel(0, 1);
    

    Or if these are the defaults for a new player, do it in the constructor and then you never need to remember.

    Hope that gets you started. Have fun and never be afraid to experiment.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just began learning Java. My friend who knows much more than me has
I am still learning C++ templates, and have encountered a problem regarding calling members
my friend and I are learning C++ together and we have a question. We
A friend told me that this a good example for learning Java scopes, but
I'm learning C++ and can't get my head around this problem: I have a
friend's I have a task to place the horizontal scroll or swipe menu tabs
My friend gave me a database file: record.mdf . I copied that .mdf file
A friend asked me a question today about an assignment problem. I found a
My friend has a website built on wordpress and the developer has left the
a friend and I develop a web application for conferences management that uses hibernate

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.