Keep an integer from going below 0?

Hello, I am writing a simple combat game and I need some help. I would like to keep my integers which I created in a couple classes from going below 0.

Here is the code.

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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#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;

	
};

void arena()

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

	cout << "Your opponent has entered the Arena!/n" << endl;

	Monster m;

	Fighter a;

	int action;

	char quit;

	do
	{

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

	cin >> action;

	switch (action)
	{
	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;
	}

	}
	while ( (a.health >= 0) || (m.health >= 0) );

	
		
		cout << "You have been defeated!\nReturn to the main menu?\nY/N\n";

		cin >> quit;
		
		switch (quit)
		{
		case 'y' :
		case 'Y' : (a.health <= 0);
			

		case 'n' :
		case 'N' : return;
		}

	

	char f;
	cin >> f;
	return;
	
}


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();
		break;

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

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

	}

	char f;
	cin >> f;
	return 0;

	
}


There are classes for Monster and Fighter right now. Inside those classes is where you find the int a.health and m.health. I tried making them unsigned int's but that just caused them to roll back to their highest possible value when it went below zero.

I don't want the health to read out, "Your health is now at: -5" or whatever it may be. I want it to stick at 0 when it reaches less than, or equal to 0.

I'm also having a hard time trying to envision how to progressively make the enemy harder. I'm still very new to C++ and coding in general, so doing something like that is probably above my current knowledge.
Last edited on
I don't want the health to read out, "Your health is now at: -5" or whatever it may be. I want it to stick at 0 when it reaches less than, or equal to 0.



1
2
3
int health;
// ... something on health
health = (health<0)?0:health;


this is known as clipping health to a minimum.
Last edited on
Thank you. Would you mind elaborating on this "health = (health<0)?0:health;"? I know that it is like a mini if, but I can't fully understand it. I understand what it does (something like, if health is less that 0, health = 0), but I don't really know how it works.



EDIT:
Wait, I took a second look at it. Is it,

If health is less than 0 make health 0 ( (health < 0)?0 ) else ( : ) keep it as health (health)?
Last edited on
Good job :D
Haha, thank you Toni, and thanks again for the help. :)
Topic archived. No new replies allowed.