C++ class variable resetting

I am creating a SpaceShip class, which has arrays for weapon names and damage, and variables for shields power, hull, engine power, and mass. I created two instances of the 'SpaceShip' class, 'ScoutShip', and 'Cruser'. When one of them calls the 'FireWeapons' function, it has the other call the 'TakeDamage' function, but for some reason the 'sheildPower' resets after 'TakeDamage' ends.

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <iostream>
#include <vector>

using namespace std;

class SpaceShip
{
    //the name of the ship
    string _name;
    //The name of your weapon and your damage
    string weapons[4];
    int weaponDamage[4];
    //Misc stats
    int enginePower, maxSheildPower, mass, hull, sheildPower ,currentUnsetWeapon = 0;

    public:

        //Are we alive?
        bool alive = true;

        void initialize(string name, int defultEnginePower, int defultSheildPower, int defultMass, int defultHull)
        {
            _name = name;
            enginePower = defultEnginePower;
            sheildPower = defultSheildPower;
            mass = defultMass;
            hull = defultHull;
        }
        void setWeapon(string name, int damage)
        {
            if (4 > currentUnsetWeapon)
            {
                weapons[currentUnsetWeapon] = name;
                weaponDamage[currentUnsetWeapon] = damage;
            }
            else
            {
                cout << "This ship has no more weapon slots\n";
            }
            currentUnsetWeapon++;
        }

        void FireWeapons(int weapon, SpaceShip target)
        {
            if(alive)
            {
                cout << _name << " has fired its " << weapons[weapon] << " at " << target._name << ".\n\n";
                target.TakeDamage(weaponDamage[weapon]);
            }
        }

        void TakeDamage(int damage)
        {
            if(alive)
            {
                if(sheildPower > 0)
                {
                    sheildPower -= damage;
                }
                else
                {
                    hull -= damage;
                }
                if(hull <= 0)
                {
                    alive = false;
                }
                cout << _name << " takes " << damage << " damage.\n" <<_name << " sheild: " << sheildPower << "\n" <<_name << " hull: " << hull << "\n\n";

                if(!alive)
                {
                    cout << _name << " has been destroied.";
                }
            }
            else
            {
                cout << "This ship has already been destroyed\n";
            }
        }
};

int main()
{
    //A Scout Ship
    SpaceShip ScoutShip;
    ScoutShip.initialize("Striker",5,1,1,2);

    ScoutShip.setWeapon("Light Lazer",1);

    ScoutShip.setWeapon("Light Lazer",1);

    ScoutShip.setWeapon("Light Lazer",1);

    ScoutShip.setWeapon("Light Missile",4);

    //A Cruser
    SpaceShip Cruser;
    Cruser.initialize("Destroyer",3,5,7,10);

    Cruser.setWeapon("Light Lazer",1);

    Cruser.setWeapon("Heavy Lazer",3);

    Cruser.setWeapon("Ion Cannon",10);

    Cruser.setWeapon("Heavy Missile",7);

    Cruser.FireWeapons(0, ScoutShip);
    ScoutShip.FireWeapons(3, Cruser);
    Cruser.FireWeapons(2, ScoutShip);

    return 0;
}
I've tried your program and it seems to work fine:

Destroyer has fired its Light Lazer at Striker.

Striker takes 1 damage.
Striker sheild: 0
Striker hull: 2

Striker has fired its Light Missile at Destroyer.

Destroyer takes 4 damage.
Destroyer sheild: 1
Destroyer hull: 10

Destroyer has fired its Ion Cannon at Striker.

Striker takes 10 damage.
Striker sheild: -9
Striker hull: 2

You haven't infected my computer with this program have you? ;)
Look closely, the striker has one shield point, then it takes one damage from the Destroyer's Light Lazer, bringing it's shield to zero. But when Striker takes 10 damage from Destroyer's Ion Cannon, its shield becomes -9, meaning that it had 1 shield point before it hit, even though it should have had zero. Which leads me to believe that 'sheildPower' is resetting.
Last edited on
On line 43, you pass target by value, which makes a copy. Pass by reference instead.

Also, use real constructors, don't use an initialize function like that.
Topic archived. No new replies allowed.