Having trouble with switch statements

I'm having trouble building a selection menu for my minesweeper game. Can anyone tell me why this jumps straight to case 2 every time, no matter what?

Example:

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
cin >> gridsize;
		switch(gridsize)
		
		
		case 1:			//EASY GRID 5 x 5 SELECTED
		{
		grid = 5;
		int n=0;
		for (n=0; n<4; n++)
		{difficulty[n] = easy[n];}
		goto noofmineselect;
		break;
		}
		case 2:      		//MEDIUM GRID 8 x 8
		{
		grid = 8;
		int n=0;
		for (n=0; n<8; n++)
		{difficulty[n] = medium[n];}
		goto noofmineselect;
		break;
		}
		case 3:			//HARD GRID 12 x 12
		{
		grid = 12;
		int n=0;
		for (n=0; n<4; n++)
		{difficulty[n] = hard[n];}
		goto noofmineselect;
		break;
		}
		default:
		cout<<"Error With Selection";
		system("pause");
		goto gridselect;
Last edited on
All the cases of the switch should be enclosed in braces. There is no need to have each case in braces.

1
2
3
4
5
6
7
8
9
10
11
switch {
     case: 1
     //your code
     break;

     case: 2
     //your code
     break


}


Is gridsize an int?

You should not use goto's - call a function instead.

Arrays must be of a const size at compile time. If you want a variable size then use a vector.

HTH
Alternative to system call at line 34:

1
2
3
std::cout << "Press \"ENTER\" to continue" << endl;
std::cin.ignore(std::numeric_limits<std::streamsize>::max, '\n');
//include limits 


Good portable way to pause the console. Ignores practically infinite amount of characters until 'ENTER' is pressed.
Topic archived. No new replies allowed.