Working on a very basic game, and I need some help

Hello everyone. I am very new to C++ (only been at it for a few days) and I am making a very basic text based fighting simulator.

I've run into numerous problems, but for the most part, I've solved them through trying different things. However, right now I am stuck and I haven't been able to solve it myself.

Basically I have a function for the "Arena", a class for the player named Fighter, a class for the monster/enemy, and a few others. In the main() function a switch statement is called with difference options like attack, block, and stuff like that. Selecting the attack option calls an attack() function in the Fighter class, which is where damage is calculated based on the players/enemy's strength ( I plan on making this more complicated in the future). The "damage" is then returned and then applied the the player/enemy's health.

The problem I am having right now is that the damage is not getting applied to the players health. It is doing the "damage" calculation in the attack() function because it reads it out with a cout, but once it gets returned back, it won't apply it to the players health. I hope I have explained this well enough, although I'm sure for most members here it's very easy to see what is happening just from looking at the code.

Here is an image of the program running. http://imgur.com/RTWSZ As you can see, it is running fine, and it even reads out the appropiate damage, but then you will notice it doesn't actually apply the damage variable to the m.health variable. It goes completely through the program as it is intended as well.


Here is the code itself.

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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include "stdafx.h"
#include <iostream>

using namespace std;



class Monster

{
	
	

public:

	int health;
	int strength;
	int damage;
	
	Monster ()
	{
		health = 20;
		strength = 20;

	}

public:

	int attackp ()
	{
		damage = strength/2;
		cout << "Monster attacked!\n You took " << damage << " damage!\n";
		
		return damage;
	}
	
};

class Fighter
{

public:

	int health;
	int strength;
	int armor;
	int experience;
	int level;


public:

	Fighter ()
	{
		health = 25;
		strength = 30;
		armor = 0;
		experience = 0;
		level = 1;
	}

	int stats()
	{
		cout << "Health is: " << health << endl;
		cout << "Strength is: " << strength << endl;
		cout << "Armor is: " << armor << endl;
		cout << "Experience is: " << experience << endl;
		cout << "Level is: " << level << endl;

		char f;
		cin >> f;
		return 0;
			
		

	}

	int attack()
	{
		int givDamage;
		givDamage = strength/2;
		cout << "You struck the enemy for " << givDamage << " damage!\n";
		return givDamage;
	}


	int getHealth ()
	{
		return health;
	}


};


class shop
{
	int potion1;
	int potion2;
	int dagger;
	int club;
	int sword;
	int gsword;

	
};

int arena(int x)

{
	
	Monster m;

	Fighter a;

	int action;

	cout << "You have entered the Arena\nPrepare for battle!\n";

	cout << "Your opponent has stepped into the Arena\n" << endl;

	cout << "(1) Attack\n";
	cout << "(2) Block\n";
	cout << "(3) Items\n";

	cin >> action;

	switch (action)
	{
	case (1) : a.attack(), m.health -= x;
		cout << "Enemy health is at: " << m.health << endl;
		m.attackp(), a.health -= x;
		cout << "Your health is now at: " << a.health << endl;
			break;

	

	char f;
	cin >> f;
	return 0;
	}
}


void exit()
{
}




int main()
{
	Fighter a;

char menu;

	cout << "Welcome to the Arena\n"
		"\nWhere would you like to go?\n"
		"(A)  Type A to go to the shop\n"
		"(B)  Type B to view your stats\n"
		"(C)  Type C to go to the arena\n"
		"(D)  Type D to exit the game\n" << endl;

	cin >> menu;

	switch (menu)
	{
	case 'a' :
	case 'A' : shop();
		break;

	case 'b' :
	case 'B' : a.stats();
		break;

	case 'c' :
	case 'C' : arena(0);
		break;

	case 'd' :
	case 'D' : exit();
		break;

	default :
		cout << "That is not a valid entry\n";
		break;

	}

	char f;
	cin >> f;
	return 0;

	
}


Please keep in mind that this is still very rough and some stuff will change before I am finished. Thanks for taking the time to read this. Hope someone can help me out!
coz

1
2
3
4
5
6
7
8

// x is zero which is coming from function parameter

case (1) : a.attack(), m.health -= x; 
		cout << "Enemy health is at: " << m.health << endl;
		m.attackp(), a.health -= x;
		cout << "Your health is now at: " << a.health << endl;
			break;


i guess u are intended to do like something below...
1
2
3
4
5
6

case (1) :  m.health -= a.attack(); 
		cout << "Enemy health is at: " << m.health << endl;
		 a.health -= m.attackp();
		cout << "Your health is now at: " << a.health << endl;
			break;
Last edited on
Ah, thank you so much! So the problem before was what? I see now, that I wasn't returning the right variable. Specifying return a.attack() will return givDamage because I specified "return givDamage;" in a.attack correct? So what was I returning before? Was it just the x variable in the arena() function, which was 0? Thanks again! :)


P.S. Is there a way to ensure an int never goes below zero? So if int x = 15, and int =y = 20, then int x - y = 0? I need this for my character's and enemy's health. I obviously don't want it to display -5 health.
Last edited on
Topic archived. No new replies allowed.