switch statement errors??

hiya

Why am i getting these errors around this function? Why does it keep saying these stuffs?


1>------ Build started: Project: makealine, Configuration: Debug Win32 ------
1>  game.cpp
1>h:\year2\console_programming_conprg\course_work\dump\makealine\makealine\game.cpp(780): error C2361: initialization of 'validpos' is skipped by 'default' label
1>          h:\year2\console_programming_conprg\course_work\dump\makealine\makealine\game.cpp(758) : see declaration of 'validpos'
1>h:\year2\console_programming_conprg\course_work\dump\makealine\makealine\game.cpp(780): error C2361: initialization of 'pathway' is skipped by 'default' label
1>          h:\year2\console_programming_conprg\course_work\dump\makealine\makealine\game.cpp(757) : see declaration of 'pathway'
1>h:\year2\console_programming_conprg\course_work\dump\makealine\makealine\game.cpp(780): error C2361: initialization of 'Move_Piece' is skipped by 'default' label
1>          h:\year2\console_programming_conprg\course_work\dump\makealine\makealine\game.cpp(754) : see declaration of 'Move_Piece'
1>h:\year2\console_programming_conprg\course_work\dump\makealine\makealine\game.cpp(780): error C2361: initialization of 'uiPiece' is skipped by 'default' label
1>          h:\year2\console_programming_conprg\course_work\dump\makealine\makealine\game.cpp(751) : see declaration of 'uiPiece'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


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
bool ComputerAI_Move()
{
	//...
	switch(Check_If_Player_Pieces_Max())
	{
	case true://Player has 3 pieces
		//////////////////////////////////////////////////
		///////////////////////////////////////////////////
		//choose piece number to move
		srand(99);
		unsigned int uiPiece = (unsigned int)(rand() % 3);

		//find piece chosen to be moved
		unsigned char Move_Piece = Cycle_to_locate_piece(uiPiece);

		//Locate valid path & make move if true
		unsigned char pathway = 0;
		unsigned char Store_valid_moves[9]; unsigned char validpos = 0;
		
		if(!find_valid_movepath(uiPiece,Move_Piece,pathway,Store_valid_moves,validpos))
		{
			//location contains no valid moves so try another location
			uiPiece = 0;//use "uiPiece" to loop through all pieces so the valid version
			//may be found
			while (uiPiece < 3)
			{
				if (find_valid_movepath(uiPiece,Move_Piece,pathway,Store_valid_moves,validpos))
				{ 
				return true; //TERMINATE FUNCTION
				//break;/*valid move found*/
				}
			
				uiPiece++; //try another piece
			}
		}

	break;
		//////////////////////////////////////////////////
		///////////////////////////////////////////////////
	default://Player has no three pieces
		 
		//Randomly_Place_Piece : returns true if valid move entered
		return Randomly_Place_Piece();
	break;
		//////////////////////////////////////////////////
		///////////////////////////////////////////////////
	}
}
What the hell are you using a switch statement for? haha

Just replace that swtich with an if and you should be good. What you are doing might be legal but I have never seen C++ like that.

The fact that your case is "true" makes it even more unconventional. So you have case "true" and case........"false"? (if , else)

Cases in C++ are not very great in terms of flexability so using them for anythin other than equal to a constant expressions are pretty useless imo.
Last edited on
replace switch with if..else.
just get rid of the embedded data declaration inside your switch statement, c++ doesn't like that.

put the following on top of your switch statement within the function

1
2
3
4
5
6
7
8
9
srand(99);
		unsigned int uiPiece = (unsigned int)(rand() % 3);

		//find piece chosen to be moved
		unsigned char Move_Piece = Cycle_to_locate_piece(uiPiece);

		//Locate valid path & make move if true
		unsigned char pathway = 0;
		unsigned char Store_valid_moves[9]; unsigned char validpos = 0;
Last edited on
Or put { } around the declarations
Topic archived. No new replies allowed.