Cannot change the value on the heap.

Feb 19, 2015 at 12:38am
//I made this game. But the crucial part doesn't work.
//The value of health doesn't decrease when the character is attacked.
//I commented the places in the code, what is related to this matter.
//Thank you for the time.

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
114
115
116
117
118
119
120
121
122
#include <iostream>

using namespace std;

void PlayerAttack();
void OpponnentAttack();
void GameLoopPlayerBegins();
void GameLoopOpponnentBegins();

int *PlayerHealthHeap();
int *OpponnentHealthHeap();

class GenericPlayer
{
public:
	void Menu();
	void Greet();

	int m_Attack;
	int m_Defense;
};


class Player : public GenericPlayer
{
public:
	void Menu();
	void Greet();

	int m_Attack = 30;
	int m_Defense = 4;
	//Health is defined '*PlayerHealthHeap()'
};



void Player::Greet()
{
	cout << "\nHi, I'm you. Well, better said. I'm your puppet." << endl;
	cout << "I have an attack of " << m_Attack << endl;
	cout << "And a defense of " << m_Defense << endl;
	cout << "And I'm going to fight that creep over there.\n";
}



class Opponnent : public GenericPlayer
{
public:
	void Script();
	void Greet();

	int m_Attack = 16;
	int m_Defense = 10;
	//Health is defined in  ' *OpponnentHealthHeap() '
};

void Opponnent::Script()
{
	Opponnent myOpponnent;
	int *pOHealth = OpponnentHealthHeap();

	int potion = 2;

	if (*pOHealth <= 60 && potion >= 0)
	{
		*pOHealth += 50;
		--potion;
	}
	else
	{
		OpponnentAttack();
	}
}

void Opponnent::Greet()
{
	cout << "\nYou trie to fight against me?";
	cout << "I have an attack of " << m_Attack << ".\n";
	cout << "And a defense of " << m_Defense << ".\n";
	cout << "Hahaha, I would like to see you trie.\n";
}

void Player::Menu()
{
	Player myPlayer;
	int *pPHealth = PlayerHealthHeap();

	int choice = 0;

	while (choice != 1 && choice != 2)
	{
		cout << "\n1 - Attack\n" << endl;
		cout << "2 - Potion\n" << endl;

		cout << "What is your answer: ";
		cin >> choice;

		switch (choice)
		{
		case 1:		// Attack;
			PlayerAttack();
			break;
		case 2:		//potion
			pPHealth += 50;        //health doesn't add 50
			cout << "The potion has add 50 health.\n";
			break;
		default: cout << "That is an illegal choice.\n";
		}

	}

}

void PlayerAttack()
{
	Opponnent myOpponnent;
	Player myPlayer;

	int *pOHealth = OpponnentHealthHeap();

	*pOHealth = *pOHealth - (myPlayer.m_Attack - myOpponnent.m_Defense); 
	cout << "\nI attacked him with my sword\n";
	cout << "I only damaged him " << (myPlayer.m_Attack - myOpponnent.m_Defense);
	cout << " because of his thick armor.\n";
}

void OpponnentAttack()
{
	Opponnent myOpponnent;
	Player myPlayer;

	int *pPHealth = PlayerHealthHeap();

---------------------------------------------- //code of health minus attack
	*pPHealth - (myOpponnent.m_Attack - myPlayer.m_Defense);
	cout << "\nI have been attacked by his sword.\n";
	cout << "He lowered my health with " << (myOpponnent.m_Attack - myPlayer.m_Defense) << "\n";
}

void GameLoopPlayerBegins()
{
	Player myPlayer;
	Opponnent myOpponnent;

	int *pPHealth = PlayerHealthHeap();
	int *pOHealth = OpponnentHealthHeap();

	while (*pPHealth != 0 && *pOHealth != 0)
	{
		myPlayer.Menu();
		myOpponnent.Script();
		cout << "My health is: " << *pPHealth << endl;
		cout << "His health is: " << *pOHealth << endl;
	}
}

void GameLoopOpponnentBegins()
{
	Player myPlayer;
	Opponnent myOpponnent;

	int *pPHealth = PlayerHealthHeap();
	int *pOHealth = OpponnentHealthHeap();

	while (*pPHealth != 0 && *pOHealth != 0)
	{
		myOpponnent.Script();
		myPlayer.Menu();
             -----------------------//still shows the begin value of 150 and 170
		cout << "My health is: " << *pPHealth << endl;
		cout << "His health is: " << *pOHealth << endl;
	}
}

int *PlayerHealthHeap()
{
  --------------------------------------//I think this is were it goes wrong
	int *pPHealth = new int(150);    ------- //Initializing health
	return pPHealth;
}

int *OpponnentHealthHeap()
{
    ------------------------- //Same here - I think this is were it goes wrong
	int *pOHealth = new int(170);   --------- //Initializing health
	return pOHealth;
}

int main()
{
	Player myPlayer;
	Opponnent myOpponnent;

	myPlayer.Greet();
	myOpponnent.Greet();

	char response;
	do
	{
		cout << "\nDo you want to begin the battle? (y/n): ";
		cin >> response;

		if (response == 'y')
		{
			GameLoopPlayerBegins();
			break;
		}
		else if (response == 'n')
		{
			GameLoopOpponnentBegins();
			break;
		}
		continue;
	} while (response != 'y' && response != 'n');

	system("pause");
	return 0;
}
Last edited on Feb 19, 2015 at 11:09am
Feb 19, 2015 at 12:55am
Please edit your post and make sure your code is [code]between code tags[/code] so that it has line numbers and syntax highlighting.

If you have any compile errors, please copy and paste the exact error messages and indicate which line they are from.
Feb 19, 2015 at 3:50am

1
2
int *pOHealth = new int(170); 
int *pPHealth = new int(150);


you cannot initialize like this this is not a constructor


Last edited on Feb 19, 2015 at 3:51am
Feb 19, 2015 at 5:53am
Lorence30 wrote:
1
2
int *pOHealth = new int(170); 
int *pPHealth = new int(150);


you cannot initialize like this this is not a constructor


Actually, that is perfectly valid code, even though it's a completely useless way to use pointers.
Feb 19, 2015 at 6:20am
closed account (D80DSL3A)
I think the problem is a matter of variable scope.
Consider the variables myOpponent and pOHealth here:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void GameLoopOpponnentBegins()
{
Player myPlayer;
Opponnent myOpponnent;

int *pPHealth = PlayerHealthHeap();
int *pOHealth = OpponnentHealthHeap();

while (*pPHealth != 0 && *pOHealth != 0)
{
myOpponnent.Script();// contains unrelated variables with the same names
myPlayer.Menu();
//-----------------------//still shows the begin value of 150 and 170
cout << "My health is: " << *pPHealth << endl;
cout << "His health is: " << *pOHealth << endl;
}
}

Within the function Script, the variables myOpponent and pOHealth are local to the function. They are unrelated to the variables by the same name outside the function.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void Opponnent::Script()
{
Opponnent myOpponnent;// not the opponent calling the function.
int *pOHealth = OpponnentHealthHeap();// not the pOHealth in the calling function, so that value is unaffected.

int potion = 2;

if (*pOHealth <= 60 && potion >= 0)
{
*pOHealth += 50;
--potion;
}
else
{
OpponnentAttack();
}
}

Without examining your code in detail (please develop and test individual features and functions as you go), I suggest modifying Script to take the pOHealth variable from outside the function, so it can be modified. Also don't declare a local opponent in the function. Let the function operate on the calling opponent object.
Eg.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void Opponnent::Script(int * pHealth)
{
int potion = 2;

if (*pHealth <= 60 && potion >= 0)
{
*pHealth += 50;
--potion;
}
else
{
OpponnentAttack();
}
}

Not sure that will be enough to fix, but should be in the right direction.
Same issue with the Player::Menu() function.

Also, please use code tags, as LB has asked.

@Lorence30. That code is fine. You can give constructor args when allocating just a single instance.
The problem arises when allocating arrays:
int* pi = new int[5];// can't give ctor args here.
Last edited on Feb 19, 2015 at 6:31am
Feb 19, 2015 at 7:21am
my bad :(
Feb 19, 2015 at 7:23am
fun2code wrote:
The problem arises when allocating arrays:
int* pi = new int[5];// can't give ctor args here


1
2
3
4
5
6
7
8
9
10
#include <iostream>
using std::cout;

int main() {	

	int *pt = new int[5]{1,2,3,4,5};
	cout << *(pt+2); //3
  
return 0;	
}
Feb 19, 2015 at 8:04am
closed account (D80DSL3A)
@anup30 Thanks. I didn't know that could be done.

I see it works for class objects fine too:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include<iostream>

class A
{
public:
    int x, y;
    A( int X, int Y ): x(X), y(Y) {}
};

int main()
{
    A* pA = new A[3]{ A(5,6), A(7,8), A(9,2) };

    std::cout << pA[0].x << ' ' << pA[1].y << '\n';

    return 0;
}
Last edited on Feb 19, 2015 at 8:13am
Feb 19, 2015 at 12:17pm
@anup30, @fun2code, @Lorence30, @LB.

The code works now. Thank you all for you your time and knowledge.
Last edited on Feb 19, 2015 at 12:18pm
Topic archived. No new replies allowed.