What is wrong?

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
//using visual studio 2010 express


#include <iostream>
#include <cstdlib> 
#include <time.h>
using namespace std;

int easygame(){
	int enemyHealth = 5;
	int hit;		
	hit = rand() % 5 + 1;
	enemyHealth = -hit + enemyHealth;
	return enemyHealth;
}
int medgame(){
	int enemyHealth = 7;			
	int hit;						
	hit = rand() % 10 + 1;
	enemyHealth = -hit + enemyHealth;
	return enemyHealth;
}
void hardgame(	unsigned int playerHealth = 10, unsigned int enemyHealth = 10){
	cout <<"enemy: "<< enemyHealth << endl;	

	cout <<"player: "<< playerHealth << endl;

	while(playerHealth>0){

		unsigned int hit = rand() % 5 + 1;

		unsigned int enemyhit = rand() % 5 + 1;

		playerHealth = playerHealth - enemyhit;

		enemyHealth = -hit + enemyHealth;
			
		cout <<"enemy: "<< enemyHealth << endl;	

		cout <<"player: "<< playerHealth << endl;
		
		system("pause");

	}
}
int main()
{
    cout <<"\tWelcome to my text based game!\n";
    char userName[100];
    cout <<"\nPlease enter your username: ";
    cin >>userName;
    cout <<"Hello, "<<userName<<"!\n\n";
    cout <<"Please pick your gender: \n";
		for (;;){
    cout <<"1 - male\n";
    cout <<"2 - female\n";
    int gender;
    cout <<"Pick your gender: ";
    cin >>gender;
    switch (gender)
	{
           case 1:
                cout <<"You are male.\n";
                break;
           case 2:
                cout <<"You are female.\n";
                break;
           default:
					cout <<"Error - Invalid input; only 1 or 2 allowed.\n";
					continue;
		 }
		break;
	}
		for (;;){
    int difficulty;
    cout <<"\nPick your level difficulty: \n";
    cout <<"1 - Easy\n";
    cout <<"2 - Medium\n";
    cout <<"3 - Hard\n";

    cout <<"Pick your level difficulty: ";
    cin >>difficulty;
    
    switch (difficulty)
    {
           case 1:
                cout <<"You picked Easy.\n\n";
				system ("cls");
                break;
           case 2:
                cout <<"You picked Medium.\n\n";
				system ("cls");
                break;
           case 3:
                cout <<"You picked Hard.\n\n";
				system ("cls");
				hardgame();
                break;
           default:
                   cout <<"Error - Invalid input; only 1,2 or 3 allowed.\n";
				   continue;
				  
		}
     break;
	}
    system("PAUSE");
	
    return 0;
}

I keep on getting really big numbers after three or four loops of "void hardgame" instead of the program ending.
I know this is probably a really simple and easy fix but I am new to C++.
What is wrong here?
You made everyone's health unsigned integers, so they can't be negative. Integers wrap, meaning that
1
2
3
unsigned x = 0;
x -= 1;
cout << x; //now x is a big number. 2^32-1, to be precise 

It would be fine if health was a multiple of damage. That way, 0 would be reached. However that is not the case and wrapping happens. The solution is to use simple (signed) int.
Thank you, but I have another problem.

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
void hardgame(int playerHealth = 10,int enemyHealth = 10){
cout << "You see an Enemy\n";

Sleep(1000);

system("cls"); 

cout <<"enemy: "<< enemyHealth << endl;	

cout <<"player: "<< playerHealth << endl;

	while (playerHealth>0,enemyHealth>0){

		int hit = rand() % 5 + 1;

		int enemyhit = rand() % 5 + 1;

		playerHealth = playerHealth - enemyhit;

		enemyHealth = -hit + enemyHealth;
			
		cout <<"enemy: "<< enemyHealth << endl;	

		cout <<"player: "<< playerHealth << endl;

if(enemyHealth = 0){
			cout << "good job\n";
			cin.get();
		}

if(playerHealth = 0){
			cout << "you died";
			cin.get();
		}
}


}



now it is ignoring the
1
2
3
4
5
6
7
8
9
if(enemyHealth = 0){
			cout << "good job\n";
			cin.get();
		}

if(playerHealth = 0){
			cout << "you died";
			cin.get();
		}



How should i fix this?
I am using this in a coordinate grid map.

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
void main(){
	int x = 10;
	Character Hero;
	Hero.itsXCoord=0;
	Hero.itsYCoord=0;
	while(x=10){
int move;
cout << "You are at :" << Hero.itsXCoord << ", " << Hero.itsYCoord << endl;
cout << "\n1)North 2)South 3)East 4)West: ";
cin >> move;
	if (move == 1){
			Hero.itsXCoord+=1;
		}
		if (move == 2){
			Hero.itsXCoord-=1;
		}
		if (move == 3){
			Hero.itsYCoord+=1;
		}
		if (move == 4){
			Hero.itsYCoord-=1;
		}
	if(Hero.itsXCoord == 3 && Hero.itsYCoord == -2){
				hardgame();
				}
		}
		  

}
Last edited on
Topic archived. No new replies allowed.