Why isn't "BoarHitPoints" reducing the way I think it should?

Apr 10, 2016 at 5:43pm
Hey, guys. I'm doing some exercises on classes in between semesters and I'm attempting to create a Combat class and a Boar class, or in a much broader sense, some enemy. The boars hit points becomes -5. I expected a result of 15, since the damage dealt is 5 and the boars hit points is 20. If boarHitPoints becomes boarHitPoints - attack, doesn't that mean 20 then becomes 15?

I'm pretty new to programming, I hope it is readable. Thanks, guys.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//Combat.cpp
#include "Combat.h"
#include "Boar.h"

Boar mottledBoar;

 void Combat::setAttackDamage(int attackDamageIn)
   {
     attack = attackDamageIn;
   }
int Combat::getAttackDamage()
    {
        return attack;
    }
int Combat::choice()
    {
    cin >> choices;
    if (Combat::choices == 1)
        mottledBoar.boarHitPoints = mottledBoar.boarHitPoints - attack;

        cout << mottledBoar.boarHitPoints;

    }


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
//main.cpp
#include <iostream>
#include <string>
#include "Boar.h"
#include "Combat.h"

using namespace std;


// making a wild boar

int main ()
{

Boar object;
Combat combatObject;
object.setBoarType("Mottled Boar");
object.setBoarHitPoints(20);
object.getBoarHitPoints();
object.setBoarHostility(true);
object.setBoarDamage(4);
combatObject.setAttackDamage(5);


cout << "A " << object.getBoarType() << " approaches." << endl;
cout << "Boars Hit Points: " <<  object.getBoarHitPoints() << endl;
cout << "Boar does " << object.getBoarDamage() << " damage. ";
cout << "Do you wish to fight this " << object.getBoarType() << "?" << endl;
cout << "1. Attack\n2. Run away" << endl;
combatObject.choice();

}

Last edited on Apr 10, 2016 at 5:48pm
Apr 10, 2016 at 5:47pm
And how do you know what the values of attack and mottledBoar.boarHitPoints are right before line 18 happens?
Apr 10, 2016 at 5:48pm
Nothing in code you've posted could, by itself, produce the effect you're seeing. Post more code.
Apr 10, 2016 at 5:53pm
And how do you know what the values of attack and mottledBoar.boarHitPoints are right before line 18 happens?


Ok, I think I know what I'm supposed to do. Am I supposed to create a class Boar, then create an object (mottledBoar) inside its own header file, include it in Combat.cpp and use those values? So for each derivative, or object of Boar.h and Boar.cpp (mottledBoar) there must be its own header file. Right?
Last edited on Apr 10, 2016 at 6:17pm
Apr 10, 2016 at 6:14pm
You are setting values in the boar object named object, but your function then goes on to operate on the boar object named mottledBoar.

They're two different boar objects.
Last edited on Apr 10, 2016 at 6:16pm
Apr 10, 2016 at 6:21pm
You are setting values in the boar object named object, but your function then goes on to operate on the boar object named mottledBoar.

They're two different boar objects.


That makes sense. Thanks. So since object is out of the scope of Combat.cpp, would it be a better idea to create a mottledBoar.h, set the values in there, then include the header into the combat.cpp?
Last edited on Apr 10, 2016 at 6:23pm
Topic archived. No new replies allowed.