Wrong Input

What's going on guys. I have this issue will my output. I'm a little confused why the output isn't what I'm expecting. Bear with me it's a bit of code.


My hero class
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
class hero
{
public:
        hero();
        void setHealth(int);
        int getHealth();
        void setPotion(int);
        int getPotion();
        void givePotion();
private:
        int health;
        int potion;
};

#include "hero.h"
#include <iostream>
using namespace std;
hero::hero()
{
        setHealth(0);
        setPotion(0);
}

void hero::setHealth(int i)
{
        health = i;
}

void hero::setPotion(int i)
{
        potion = i;

}


int hero::getHealth()
{
        return health;
}

int hero::getPotion()
{
        return potion;

}

void hero::givePotion()
{
        char choice;
        if(getHealth() < 50)
        cout << "Would you like to use a potion?(Y/N)" << endl;
        cin >> choice;
        if(choice == 'Y')
        {
                setHealth(getHealth() + 50);
                potion = potion - 1;
        }
}


My enemy class

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
  
#include "hero.h"

class enemy: public hero
{
public:
    enemy();
    void attack();
    void setDamage(int);
    int getDamage();

private:
    int damage;
};


#include <iostream>
#include "enemy.h"


using namespace std;
enemy::enemy()
{
        setDamage(10);
}

void enemy::setDamage(int d)
{
        damage = d;
}

int enemy::getDamage()
{
        return damage;
}
void enemy::attack() //This function calls the enemy to attack the hero
{
        cout << "Enemy has Attacked! - " << getDamage() << endl;
        cout << getHealth() << endl; //I want 100, but it's 0
        int x = getHealth() - getDamage();
        setHealth(x);
}






My main file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  #include <iostream>
#include <string>
#include "enemy.h"
using namespace std;

int main()
{
        hero h;
        enemy e;
        h.setHealth(100);
        e.setDamage(10);
        e.attack();
        cout << h.getHealth() << endl;
        return 0;
}


My output is

Enemy has attacked! - 10
-10 // I want health to be 90

I was thinking it has to do with the default constructor setting everything to 0, but shouldn't that be overwritten when I called the setHealth function in the main program? Thanks
Last edited on
closed account (48T7M4Gy)
The problem is twofold:
1. It is bad class design to directly display results. i.e. it is good practice to return values and not use the class to cin or cout. In this case it adds to the confusion. main is the right place for cout's and cin's.

2. Your implementation quite rightly differentiates hero's and enemy's but then goes on to confuse enemy::attack() by not clearly defining who is the attacker and who is the defender/hero. What this means is the enemy attack method should include a reference (parameter) regarding the identity of the hero. That way the hero's properties are amended, not the attackers. Should be something like enemy::attack( hero aHero, int someDamage);
My output is
Enemy has attacked! - 10
-10 // I want health to be 90


cout << getHealth() << endl; //I want 100, but it's 0
decide yourself


> e.attack();
¿attack to whom?

> the default constructor setting everything to 0, but shouldn't that be
> overwritten when I called the setHealth function in the main program?
¿where do you set the health of the enemy `e'?
Enemy class
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
#include "hero.h"

class enemy
{
public:
	enemy();
	void attack(hero &h);
	void setDamage(int);
	int getDamage();

private:
	int damage;
};

#include <iostream>
#include "enemy.h"


using namespace std;
enemy::enemy()
{
	setDamage(10);
}

void enemy::setDamage(int d)
{
	damage = d;
}

int enemy::getDamage()
{
	return damage;
}
void enemy::attack(hero &h) //This function calls the enemy to attack the hero
{
	cout << "Enemy has Attacked! - " << getDamage() << endl;
	cout << h.getHealth() << endl; //I want 100, but it's 0
	int x = h.getHealth() - getDamage();
	h.setHealth(x);
}


Main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>
#include "enemy.h"
using namespace std;

int main()
{
	hero h;
	enemy e;
	h.setHealth(100);
	e.setDamage(10);
	e.attack(h);
	cout << h.getHealth() << endl;

	return 0;
}


You have to pass by reference the object you want to perform the action on. Otherwise the compiler doesn't know you are trying to change the values of hero h in main and not some other hero.

Also, like others have said, this is a poorly designed class. You should consider rewriting it. But if you must use it this way, this is how you do it.
Last edited on
Ok I'll rewrite it. Anything else besides the cins and couts?
Thanks for the tips.
Also the cout << getHealth() << endl;
is not part of the final program. I just put it in to as a reference.
Last edited on
closed account (48T7M4Gy)
Ok I'll rewrite it.
Excellent. It is only a couple of lines. So you don't have to scrap anything much.

Anything else besides the cins and couts?
As long as you make the other changes to attack( ... ) you should get sensible results.
( Test out as an exercise for yourself whether it should be hero&, hero or const hero@, const hero in the parameter list. It's important to get that right. )

( The question also arises when a hero attacks an enemy )
Topic archived. No new replies allowed.