I need help with my loop

My main proble is that I want the used to be able to go back and fourth between places but Im not sure how I would go about that

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
#include <iostream>
#include "windows.h"
#include <fstream>

using namespace std;

void gexit ()//this is the exit function
{
	cout << "\nYou are dead.............Sorry\n";
	system("pause");
	exit (0);
}
int hp = 10;
int mana = 10;
int atk = 10;
int gold = 2;
int lvl = 0;
int xp = 0;
int level = 1;
bool gamestop = false;
int choice;

int main()
{
	while (gamestop == false && level == 3)
	{
		system ("cls");
		cout << "\n\nThis is the shop" << endl;
		cout << "\nBuy somthing or get the feck out!                      Level:  " << lvl;
		cout << "\n                                                       Hp:  " << hp;
		cout << "\n(1) Buy health Potion (1g)                             Attack:  " << atk;
		cout << "\n(2) Get the feck out                                   Gold:  " << gold << "\n";
		cin >> choice;
		switch (choice)
		{
		case 1:
			{
				gold = gold - 1;
				hp = hp + 1;
				level = 1;
			}
		case 2:
			{
				system ("cls");
				level = 1;
			}
		}
	}
	while (gamestop == false && level == 2)
	{
		system ("cls");
		cout << "This is the arena" << endl;
		
	}
	while  (gamestop == false && level == 1)
	{
		cout << "\n\nYou are in town. Were would you like to go?";
		cout << "\n                                                       Level:  " << lvl;
		cout << "\n                                                       Hp:  " << hp;
		cout << "\n(1) Fight Arena                                        Attack:  " << atk;
		cout << "\n(2) Shop                                               Gold:  " << gold << "\n";                                                 
		cin >> choice;
		switch (choice)
		{
		case 1:
			{
				level = 2;
			}break;
		case 2:
			{
				level = 3;
			}break;
		}
	
	}
	while (gamestop == false && level == 2)
	{
		system ("cls");
		cout << "This is the arena" << endl;
		
	}
	
	while (hp >= 0)
	{
		if (hp <= 0)
		{
			gexit;
		}
	}
	while (xp >= 0)
	{
		if (xp == 5)
		{
			lvl = lvl + 1;
		    atk = atk + 1;
		}
		if (xp == 10)
		{
			lvl = lvl + 1;
			atk = atk + 1;
		}
		if (xp == 15)
		{
			lvl = lvl + 1;
			atk = atk + 1;
		}
		if (xp == 20)
		{
			lvl = lvl + 1;
			atk = atk + 1;
		}
		if (xp == 25)
		{
			lvl = lvl + 1;
			atk = atk + 1;
		}
		if (xp == 30)
		{
			lvl = lvl + 1;
			atk = atk + 1;
		}
		if (xp == 35)
		{
			lvl = lvl + 1;
			atk = atk + 1;
		}
		if (xp == 40)
		{
			lvl = lvl + 1;
			atk = atk + 1;
		}
		if (xp == 45)
		{
			lvl = lvl + 1;
			atk = atk + 1;
		}
		if (xp == 50)
		{
			lvl = lvl + 1;
			atk = atk + 1;
		}
	}
	return 0;
}



Any help will be much appropriated.
Not related to you question, but you have a couple of issues.

Lines 85-88: The < condition will never be true because of the while condition.

Line 87: This is not a function call. For a function call you need ().
 
  gexit();


Line 92-141: You can replace all these lines with the following:
1
2
3
4
    if (xp % 5 == 0)  // is xp a multiple of 5?
    {   lvl = lvl + 1;
        atk = atk + 1;
    }
thanks man, I am only doing this as a side prject so I would think there would be alot of errors. If you could help me with my first problem it would be much appreciated but anyway thanks alot :D
I would suggest putting lines 75-86 into a function. Then call that function any time you want to allow the user to go somewhere. I would suggest changing the variable named level to location. This would be more indicative of it's use. level is easily confused with lvl.

To give you an idea:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
const int max_locations = 3;
const string loc_names[max_locations] = { "Town", "Arena", "Shop" };

int move_to_location (int curr_loc)
{  int loc;
    cout << "Where do you want to go?";
    for (int i=0; i<max_locations; i++)
        if (i != curr_loc)  // Don't display if already there
            cout << i << " " << loc_names[i] << endl;
    cin >> loc;
    while (loc < 0 || loc >= max_locations || loc == curr_loc)
    {  cout << "Not a valid location.  Try again" << endl;
        cin >> loc;
    }
    cout << "You are in " << loc_names[loc] << endl;
    return loc;  // caller will update curr_location from returned value
}


thanks that really helps alot :D I only have two wuestions, 1. whats does const mean? and 2. I am going to try to get the player to fight in the arena but I want the enemys to ranomly generate so for arguements sake lets say there are 5 diffrent enemys how do I randomly get one to fight the player? thanks alot man your helping me out alot :D
1) The const modifier means that the value can not be changed.

2)
1
2
3
4
5
6
7
//  At the beginning of main
  srand(time(NULL));  //  Initialize the random number generator

//  Where you want the fight to happen
  const int max_enemies = 5;
  int enemy;
  enemy = rand() % max_enemies;  //  Generate a random value from 0 - 4 


Thank alot dude, really helped me out :D
Topic archived. No new replies allowed.