171 7 [Error] expected unqualified-id before 'while'

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
#include <iostream>
#include <Windows.h>
using namespace std;
char Map[10][10] = {"#########",
	                "#  #   !#",
					"#  # ####",
					"#### #  #",
					"#    #  #",
					"# ####  #",
					"# #     #",
					"# #     #",
					"#@#     #",
					"#########"};
int Gamespeed = 100;
int Level = 1;
bool stopgame = false;
int Hp = 100;
int MaxHp = 100;

int main()
{

	while(stopgame == false && Level == 1)
	{
	
		system("cls");
		for (int y = 0; y < 10; y++)
		{
	
			 cout << Map[y] << endl; 
	}
	cout << "Hp: " << Hp << "/" << MaxHp << endl;
	for (int y = 0; y<10; y++)
	{
	    for (int x = 0; x<10; x++)
		{


			switch(Map[y][x])
			{
			case '#':	
			{
				Map[y][x] = 219;
			}
			case '@':
			{
				
				
				if (GetAsyncKeyState(VK_UP) != 0 )
				{
				    int y2 = (y-1);

                     switch(Map[y2][x])
					{
					case ' ':
				    
					{
						Map[y][x] = ' ';
						y -= 1;
                        Map[y2][x] = '@';
					}break;
					case '!':
						{
							Level = 2;
						}break;
						case '*':
						{
							Hp -= 20;
							Map[y][x] = ' ';
							y -= 1;
                        	Map[y2][x] = '@';	
						}break;
				}
			}
			
			if (GetAsyncKeyState (VK_DOWN) != 0)
			{
				int y2 = (y + 1);
				switch (Map [y2][x])
				{
				case ' ':
					
					
				{
					Map[y][x] = ' ';
						y += 1;
                        Map[y2][x] = '@';
				}break;
				case '!':
					{
						Level = 2;
					}break;
					case '*':
					{
						Hp -= 20;
						Map[y][x] = ' ';
						y += 1;
                       	Map[y2][x] = '@';
					}break;
				
				
				}	
					
					}
			}
			
			if (GetAsyncKeyState(VK_RIGHT) != 0)
			{
			
			int x2 = (x + 1);
			switch(Map[y][x2])
			{
				case ' ':
				{
					    Map[y][x] = ' ';
						x += 1;
                        Map[y][x2] = '@';
				}break;
			    case '!':
			    	{
			    		Level = 2;
			    	}break;
			    	case '*':
			    	{
			    		Hp -= 20;
						Map[y][x] = ' ';
						x += 1;
                        Map[y][x2] = '@';
			    	}break;
			    	
			    	
			    	
			} 
		}
		
		if (GetAsyncKeyState(VK_LEFT)!= 0)
		{
			int x2 = (x - 1);
			
			switch(Map[y][x2])
			{
				case ' ':
			
			{
				        Map[y][x] = ' ';
						x -= 1;
                        Map[y][x2] = '@';
			}break;
	        case '!':
	        {
	        	Level = 2;
	        }break;
	        case '*':
	        {
	        	Hp -= 20;
				Map[y][x] = ' ';
				x -= 1;
                Map[y][x2] = '@';	
	       	}break;
	        
	        
		}   
		
	}break;
			Sleep(Gamespeed);
			}
			}
			}
			}	
			}
		    while (stopgame == false && Level == 2) {
		    
			{
				system("cls");
				cout << "Level 2 script goes here....\n\n";
				system("pause");
				return EXIT_SUCCESS;
 								}
return 0;}
	
Last edited on
Let me guess, correct indentation and function calls are for whimps?
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
#include <iostream>
#include <Windows.h>

using namespace std;

char Map[10][10] =
{
	"#########",
	"#  #   !#",
	"#  # ####",
	"#### #  #",
	"#    #  #",
	"# ####  #",
	"# #     #",
	"# #     #",
	"#@#     #",
	"#########"
};

int Gamespeed = 100;
int Level = 1;
bool stopgame = false;
int Hp = 100;
int MaxHp = 100;

int main()
{
	while(stopgame == false && Level == 1)
	{	
		system("cls");
		for (int y = 0; y < 10; y++)
		{
			cout << Map[y] << endl; 
		}

		cout << "Hp: " << Hp << "/" << MaxHp << endl;
		for (int y = 0; y<10; y++)
		{
			for (int x = 0; x<10; x++)
			{
				switch(Map[y][x])
				{
					case '#':	
					{
						Map[y][x] = 219;
					}
					// kbw: drop thru?

					case '@':
					{
						if (GetAsyncKeyState(VK_UP) != 0 )
						{
							int y2 = (y-1);

							switch(Map[y2][x])
							{
							case ' ':
								{
									Map[y][x] = ' ';
									y -= 1;
									Map[y2][x] = '@';
								}
								break;
							case '!':
								{
									Level = 2;
								}
								break;
							case '*':
								{
									Hp -= 20;
									Map[y][x] = ' ';
									y -= 1;
									Map[y2][x] = '@';	
								}
								break;
							}
						}

						if (GetAsyncKeyState (VK_DOWN) != 0)
						{
							int y2 = (y + 1);

							switch (Map [y2][x])
							{
							case ' ':
								{
									Map[y][x] = ' ';
									y += 1;
									Map[y2][x] = '@';
								}
								break;
							case '!':
								{
									Level = 2;
								}
								break;
							case '*':
								{
									Hp -= 20;
									Map[y][x] = ' ';
									y += 1;
									Map[y2][x] = '@';
								}
								break;
							}
						}
					}

					if (GetAsyncKeyState(VK_RIGHT) != 0)
					{
						int x2 = (x + 1);

						switch(Map[y][x2])
						{
						case ' ':
							{
								Map[y][x] = ' ';
								x += 1;
								Map[y][x2] = '@';
							}
							break;
						case '!':
							{
								Level = 2;
							}
							break;
						case '*':
							{
								Hp -= 20;
								Map[y][x] = ' ';
								x += 1;
								Map[y][x2] = '@';
							}
							break;
						} 
					}

					if (GetAsyncKeyState(VK_LEFT)!= 0)
					{
						int x2 = (x - 1);

						switch(Map[y][x2])
						{
						case ' ':
							{
								Map[y][x] = ' ';
								x -= 1;
								Map[y][x2] = '@';
							}
							break;
						case '!':
							{
								Level = 2;
							}
							break;
						case '*':
							{
								Hp -= 20;
								Map[y][x] = ' ';
								x -= 1;
								Map[y][x2] = '@';	
							}
							break;
						}
					}
					break;
					Sleep(Gamespeed); // kbw: what's going on here?
				}
			}
		}
	}	
}	// kbw: what's going on down here?
while (stopgame == false && Level == 2)
{
	{
		system("cls");
		cout << "Level 2 script goes here....\n\n";
		system("pause");
		return EXIT_SUCCESS;
	}

	return 0;
}
Last edited on
How i fix it?????????
i am noob
The idea is when too much code accumulates, it's difficult to understand it. The process of breaking it up is generally called refactoring.

I've just moved the move code into seperate functions. The code compiles (using Visual C++), but I haven't tried to run it.

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
#include <Windows.h>
#include <iostream>

void MoveUp(int x, int y);
void MoveDown(int x, int y);
void MoveRight(int x, int y);
void MoveLeft(int x, int y);

using namespace std;

char Map[10][10] =
{
	"#########",
	"#  #   !#",
	"#  # ####",
	"#### #  #",
	"#    #  #",
	"# ####  #",
	"# #     #",
	"# #     #",
	"#@#     #",
	"#########"
};

int Gamespeed = 100;
int Level = 1;
bool stopgame = false;
int Hp = 100;
int MaxHp = 100;

int main()
{
	while(stopgame == false && Level == 1)
	{	
		system("cls");
		for (int y = 0; y < 10; y++)
		{
			cout << Map[y] << endl; 
		}

		cout << "Hp: " << Hp << "/" << MaxHp << endl;
		for (int y = 0; y<10; y++)
		{
			for (int x = 0; x<10; x++)
			{
				switch(Map[y][x])
				{
				case '#':	
					{
						Map[y][x] = 219;
					}
					break;

				case '@':
					{
						if (GetAsyncKeyState(VK_UP) != 0 )
						{
							MoveUp(x, y);
						}
						else if (GetAsyncKeyState (VK_DOWN) != 0)
						{
							MoveDown(x, y);
						}
						else if (GetAsyncKeyState(VK_RIGHT) != 0)
						{
							MoveRight(x, y);
						}
						else if (GetAsyncKeyState(VK_LEFT)!= 0)
						{
							MoveLeft(x, y);
						}
					}
					break;
				}
			}
		}

		Sleep(Gamespeed);
	}

	while (stopgame == false && Level == 2)
	{
		system("cls");
		cout << "Level 2 script goes here....\n\n";
		system("pause");
		return EXIT_SUCCESS;
	}

	return 0;
}

void MoveUp(int x, int y)
{
	int y2 = (y-1);

	switch(Map[y2][x])
	{
	case ' ':
		{
			Map[y][x] = ' ';
			y -= 1;
			Map[y2][x] = '@';
		}
		break;
	case '!':
		{
			Level = 2;
		}
		break;
	case '*':
		{
			Hp -= 20;
			Map[y][x] = ' ';
			y -= 1;
			Map[y2][x] = '@';	
		}
		break;
	}
}

void MoveDown(int x, int y)
{
	int y2 = (y + 1);

	switch (Map [y2][x])
	{
	case ' ':
		{
			Map[y][x] = ' ';
			y += 1;
			Map[y2][x] = '@';
		}
		break;
	case '!':
		{
			Level = 2;
		}
		break;
	case '*':
		{
			Hp -= 20;
			Map[y][x] = ' ';
			y += 1;
			Map[y2][x] = '@';
		}
		break;
	}
}

void MoveRight(int x, int y)
{
	int x2 = (x + 1);

	switch(Map[y][x2])
	{
	case ' ':
		{
			Map[y][x] = ' ';
			x += 1;
			Map[y][x2] = '@';
		}
		break;
	case '!':
		{
			Level = 2;
		}
		break;
	case '*':
		{
			Hp -= 20;
			Map[y][x] = ' ';
			x += 1;
			Map[y][x2] = '@';
		}
		break;
	} 
}

void MoveLeft(int x, int y)
{
	int x2 = (x - 1);

	switch(Map[y][x2])
	{
	case ' ':
		{
			Map[y][x] = ' ';
			x -= 1;
			Map[y][x2] = '@';
		}
		break;
	case '!':
		{
			Level = 2;
		}
		break;
	case '*':
		{
			Hp -= 20;
			Map[y][x] = ' ';
			x -= 1;
			Map[y][x2] = '@';	
		}
		break;
	}
}
Last edited on
How i fix it?????????

Did you look at the reformatted code kbw posted, with his comments showing you where the problems are?

Once the code has been properly laid out, it's obvious that you've got your open-braces and close-braces inconsistent. On line 171 of your original code - line 173 of kbw's first post - you can see that you've put in a close-brace that closes your main function, but then you have more code after that main function. It looks as though you've either added one too many close-braces, or else missed out an open-brace.

You need to look over your code, and work out which of those two things is the case, and then adjust the number of braces so that it's correct.

kbw's advice is good, and you should follow it:

1) Use a sensible, consistent indentation style, so that you can easily see where you've made mistakes

2) Learn to break up your code into self-contained functions, so that you don't wind up with complicated, deeply-nested code.
Last edited on
thank you
can sombody help me with my player @
its slide right and down
Topic archived. No new replies allowed.