Game closing after killing one monster.

Hello, I am having a problem... I posted here before and got some help with my game, but now a whole new problem has aroused. Everything works perfectly fine on my game so far, except after you kill one enemy the game closes, instead of returning back to the part where it asks you what you want to do. If anybody could help me out please do. My game is also a text based RPG, just to let you know :P.

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
#include <iostream>
#include <string>
using namespace std;

int main()

{
    int strength, spellPower, health, playerLevel, choose, playerXp, arrowPower, gold;
    int evilStrength, evilSpellPower, evilHealth, evilLevel;
    playerLevel = 1;
    playerXp = 0;
    cout << "Welcome to Crypt!" << endl;
    cout << "A console RPG game where you choose your playstyle and fight in an arena." << endl;
    cout << "Choose a class." << endl;
    cout << "1. Knight" << endl;
    cout << "2. Archer" << endl;
    cout << "3. Spellbreaker" << endl;
    cout << "4. Healer" << endl;
    cout << "5. Zombie" << endl;
    int playerClass;
    cin >> playerClass;
    if (playerClass == 1)

    {
        cout << "     ===|====>" << endl;
        strength = 10;
        spellPower = 0;
        health = 100;
        cout << "you choose The Knight! here is your attributes:" << endl;
        cout << "Strength = " << strength << endl << "Health = " << health << endl;
    }
    else if (playerClass == 2)
    {
        cout << "     |)" << endl;
        strength = 8;
        arrowPower = 25;
        health = 90;
        cout << "you choose the Archer! here is your attributes:" << endl;
        cout << "Strength = " << strength << endl << "Bow and Arrow = " << arrowPower << endl << "Health = " << health << endl;
    }
    else if (playerClass == 3)
    {
        cout << "     ----(o" << endl;
        strength = 7;
        spellPower = 20;
        health = 95;
        cout << "you choose the Spellbreaker! here is your attributes:" << endl;
        cout << "Strenght = " << strength << endl << "Spell Power = " << spellPower << endl << "Health = " << health << endl;
    }
    else if (playerClass == 4)
    {
        cout << "     <3" << endl;
        strength = 5;
        spellPower = 18;
        health = 150;
        cout << "you choose the Healer! here is your attributes:" << endl;
        cout << "Strength = " << strength << endl << "Spell Power = " << spellPower << endl << "Heatlh = " << health << endl;
    }
    else if (playerClass == 5)
    {
        cout << "     brrraiiinssss...." << endl;
        strength = 9;
        spellPower = 0;
        health = 90;
        cout << "you choose the Zombie! here is your attributes:" << endl;
        cout << "Strength = " << strength << endl << "Spell Power = " << spellPower << endl << "Heatlh = " << health << endl;
    }

{
  gold = 0;
}


bool gameOver = false;
    while (!gameOver)
    if (playerLevel < 5)
        if (health == 0)
        gameOver = true;


    {


        cout << "|_|_|_|_|__|__|__|_|_|_|_|" << endl;
        cout << "|_______|________|_______|" << endl;
        cout << "|  ___  |        |  ___  |" << endl;
        cout << "| [   ] |        | [   ] |" << endl;
        cout << "| [___] |  ____  | [___] |" << endl;
        cout << "|       | |    | |       |" << endl;
        cout << "|       | |    | |       |" << endl;
        cout << "--------------------------" << endl;
        cout << "Welcome to the Crypt Arena!" << endl;
        cout << "You are level " << playerLevel <<endl;
        cout << "You have " << gold << " Gold" << endl;
        cout << "---------------------------" << endl;
        cout << "What do you want to fight?" << endl << "\n 1. Bandit" << endl << "\n 2. Troll" << endl;
        cin >> choose;

    }



        if (choose == 1)

        {
            {
                cout << string( 100, '\n' );
                cout << "fighting level 1 Bandit!! Prepare for combat!" << endl;
                evilHealth = 40;
                evilStrength = 4;
                evilSpellPower = 0;


                while (health > 0)
                {
                if (evilHealth < 0)
                {
                    playerXp += 10;
                    gold += 5;
                    cout << "You killed the Bandit! You are awarded 10 experience points! you now have " << playerXp << " experience points!" << endl;
                    cout << "You are awarded 5 Gold! You now have " << gold << " Gold!" << endl;
                    cout << "Type any number to continue." << endl;
                    cin >> choose;
                    cout << string( 100, '\n' );

                    break;
                }
                health -= evilStrength * 2;
                cout << "the Bandit strikes and deals " << evilStrength * 2 << " damage to you!" << "\nYou have " << health << " health left!" << endl;
                cout << "Choose an action!" << "\n 1. Attack" << "\ 2. Cast fireball" << endl;
                cin >> choose;
                if (choose == 1)
                {
                    evilHealth -= strength * 2;
                    cout << "you swing your weapon and deal " << strength * 2 << " damage to the Bandit!" << endl;
                    cout << "the Bandit has " << evilHealth << " health left!" << endl;
                }
                if (choose == 2)
                {
                     evilHealth -= strength * 2;
                    cout << "You cast a fireball and deal " << strength * 2 << " damage to the Bandit!" << endl;
                    cout << "the Bandit has " << evilHealth << " health left!" << endl;
                }

                }
                }

        }



            if (choose == 2)
            {

                {
                    cout << string( 100, '\n' );
                cout << "fighting level 1 Troll!! Prepare for combat!" << endl;
                evilLevel = 2;
                evilHealth = 40;
                evilStrength = 4;
                evilSpellPower = 0;


                while (health > 0)
                {
                if (evilHealth < 0)
                {
                    playerXp += 10;
                    cout << "You killed the Troll! You are awarded 10 experience points! you now have " << playerXp << " experience points!" << endl;
                    cout << "Type any number to continue." << endl;
                    cin >> choose;
                    cout << string( 100, '\n' );

                    break;
                    }
                    health -= evilStrength * 2;
                cout << "the Troll strikes and deals " << evilStrength * 2 << " damage to you!" << "\nYou have " << health << " health left!" << endl;
                cout << "Choose an action!" << "\n 1. Attack" << "\ 2. Cast fireball" << endl;
                cin >> choose;
                if (choose == 1)
                {
                     evilHealth -= strength * 2;
                    cout << "you swing your weapon and deal " << strength * 2 << " damage to the Troll!" << endl;
                    cout << "the Troll has " << evilHealth << " health left!" << endl;
                }

        }
        }
        }
    return 0;
    }
You have a problem with your loop.
1
2
3
4
5
 
while (!gameOver)
    if (playerLevel < 5)
        if (health == 0)
        gameOver = true;


that is just looping your if statement not anything else, and since the level isn't changing(at this point) it goes on for ever.
Last edited on
Hm, I tryed moving it to the end, didn't help, I also tryed to change if (playerLevel < 5) to if (playerLevel < 0) and didn't do anything. How do I fix this? :/. I am pretty new to this stuff.
whats making your player level rise...i cant see it
Devon, it is right here: if (choose == 1)

{
{
cout << string( 100, '\n' );
cout << "fighting level 1 Bandit!! Prepare for combat!" << endl;
evilHealth = 40;
evilStrength = 4;
evilSpellPower = 0;


while (health > 0)
{
if (evilHealth < 0)
{
playerXp += 10;
gold += 5;
cout << "You killed the Bandit! You are awarded 10 experience points! you now have " << playerXp << " experience points!" << endl;
cout << "You are awarded 5 Gold! You now have " << gold << " Gold!" << endl;
cout << "Type any number to continue." << endl;
cin >> choose;
cout << string( 100, '\n' );
There are a couple problems with your code such as the level rise. but follow my comments to fix the loop.

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
#include <iostream>
#include <string>
using namespace std;

int main()

{
    int strength, spellPower, health, playerLevel, choose, playerXp, arrowPower, gold;
    int evilStrength, evilSpellPower, evilHealth, evilLevel;
    playerLevel = 1;
    playerXp = 0;
    cout << "Welcome to Crypt!" << endl;
    cout << "A console RPG game where you choose your playstyle and fight in an arena." << endl;
    cout << "Choose a class." << endl;
    cout << "1. Knight" << endl;
    cout << "2. Archer" << endl;
    cout << "3. Spellbreaker" << endl;
    cout << "4. Healer" << endl;
    cout << "5. Zombie" << endl;
    int playerClass;
    cin >> playerClass;
    if (playerClass == 1)

    {
        cout << "     ===|====>" << endl;
        strength = 10;
        spellPower = 0;
        health = 100;
        cout << "you choose The Knight! here is your attributes:" << endl;
        cout << "Strength = " << strength << endl << "Health = " << health << endl;
    }
    else if (playerClass == 2)
    {
        cout << "     |)" << endl;
        strength = 8;
        arrowPower = 25;
        health = 90;
        cout << "you choose the Archer! here is your attributes:" << endl;
        cout << "Strength = " << strength << endl << "Bow and Arrow = " << arrowPower << endl << "Health = " << health << endl;
    }
    else if (playerClass == 3)
    {
        cout << "     ----(o" << endl;
        strength = 7;
        spellPower = 20;
        health = 95;
        cout << "you choose the Spellbreaker! here is your attributes:" << endl;
        cout << "Strenght = " << strength << endl << "Spell Power = " << spellPower << endl << "Health = " << health << endl;
    }
    else if (playerClass == 4)
    {
        cout << "     <3" << endl;
        strength = 5;
        spellPower = 18;
        health = 150;
        cout << "you choose the Healer! here is your attributes:" << endl;
        cout << "Strength = " << strength << endl << "Spell Power = " << spellPower << endl << "Heatlh = " << health << endl;
    }
    else if (playerClass == 5)
    {
        cout << "     brrraiiinssss...." << endl;
        strength = 9;
        spellPower = 0;
        health = 90;
        cout << "you choose the Zombie! here is your attributes:" << endl;
        cout << "Strength = " << strength << endl << "Spell Power = " << spellPower << endl << "Heatlh = " << health << endl;
    }

{
  gold = 0;
}


bool gameOver = false;

//this loop is wrong to fix follow comments
    while (!gameOver)
		// add a left brace here  {
	
    if (playerLevel < 5)
        if (health == 0)
        gameOver = true;

//remove this left brace
    {


        cout << "|_|_|_|_|__|__|__|_|_|_|_|" << endl;
        cout << "|_______|________|_______|" << endl;
        cout << "|  ___  |        |  ___  |" << endl;
        cout << "| [   ] |        | [   ] |" << endl;
        cout << "| [___] |  ____  | [___] |" << endl;
        cout << "|       | |    | |       |" << endl;
        cout << "|       | |    | |       |" << endl;
        cout << "--------------------------" << endl;
        cout << "Welcome to the Crypt Arena!" << endl;
        cout << "You are level " << playerLevel <<endl;
        cout << "You have " << gold << " Gold" << endl;
        cout << "---------------------------" << endl;
        cout << "What do you want to fight?" << endl << "\n 1. Bandit" << endl << "\n 2. Troll" << endl;
        cin >> choose;

  //remove the brace below and add it to the end
	}



        if (choose == 1)

        {
            {
                cout << string( 100, '\n' );
                cout << "fighting level 1 Bandit!! Prepare for combat!" << endl;
                evilHealth = 40;
                evilStrength = 4;
                evilSpellPower = 0;


                while (health > 0)
                {
                if (evilHealth < 0)
                {
                    playerXp += 10;
                    gold += 5;
                    cout << "You killed the Bandit! You are awarded 10 experience points! you now have " << playerXp << " experience points!" << endl;
                    cout << "You are awarded 5 Gold! You now have " << gold << " Gold!" << endl;
                    cout << "Type any number to continue." << endl;
                    cin >> choose;
                    cout << string( 100, '\n' );

                    break;
                }
                health -= evilStrength * 2;
                cout << "the Bandit strikes and deals " << evilStrength * 2 << " damage to you!" << "\nYou have " << health << " health left!" << endl;
                cout << "Choose an action!" << "\n 1. Attack" << "\ 2. Cast fireball" << endl;
                cin >> choose;
                if (choose == 1)
                {
                    evilHealth -= strength * 2;
                    cout << "you swing your weapon and deal " << strength * 2 << " damage to the Bandit!" << endl;
                    cout << "the Bandit has " << evilHealth << " health left!" << endl;
                }
                if (choose == 2)
                {
                     evilHealth -= strength * 2;
                    cout << "You cast a fireball and deal " << strength * 2 << " damage to the Bandit!" << endl;
                    cout << "the Bandit has " << evilHealth << " health left!" << endl;
                }

                }
                }

        }



            if (choose == 2)
            {

                {
                    cout << string( 100, '\n' );
                cout << "fighting level 1 Troll!! Prepare for combat!" << endl;
                evilLevel = 2;
                evilHealth = 40;
                evilStrength = 4;
                evilSpellPower = 0;


                while (health > 0)
                {
                if (evilHealth < 0)
                {
                    playerXp += 10;
                    cout << "You killed the Troll! You are awarded 10 experience points! you now have " << playerXp << " experience points!" << endl;
                    cout << "Type any number to continue." << endl;
                    cin >> choose;
                    cout << string( 100, '\n' );

                    break;
                    }
                    health -= evilStrength * 2;
                cout << "the Troll strikes and deals " << evilStrength * 2 << " damage to you!" << "\nYou have " << health << " health left!" << endl;
                cout << "Choose an action!" << "\n 1. Attack" << "\ 2. Cast fireball" << endl;
                cin >> choose;
                if (choose == 1)
                {
                     evilHealth -= strength * 2;
                    cout << "you swing your weapon and deal " << strength * 2 << " damage to the Troll!" << endl;
                    cout << "the Troll has " << evilHealth << " health left!" << endl;
                }

        }
        }
        }
			//so add right brace here }
    return 0;
    }
Last edited on
I tryed to do what you said Binary, but when I try'd to play it all it let me do was choose my class and then said press any key to continue, and it closes.
Almost missed this.

You should have brought an open block-brace('{') after the while statement, and end it with ('}') before the return statement in function main()

HTH,
Aceix.
Last edited on
Ace, I don't fully understand what you are trying to tell me.
Do I just put the after the while statement at bool, and then end it before return 0;?
Yes.
Im a beginner, even more so than you so i prolly just missed this, but how does playerxp make player level rise? even for my own benefit? fanks :)
Because you make the playerlevel go up when you have a certain amount of playerxp ;).
what lines that on tho
Here is another hint - you can build up the cout statements in parts - so instead of having a huge long cout, do it in bits like this:

1
2
3
4
cout << "You killed the Troll! You are awarded 10 ";
cout << "experience points! you now have " ;
cout << playerXp << " experience points!" << endl;


I find it a bit of a pain to have to scroll sideways all the time to read code. There is a bit of a rule that code should not exceed 80 chars per line.

My other comment is that the code would benefit from splitting it into functions - you have 200 lines of code all in main.

Hope all goes well.
Topic archived. No new replies allowed.