problem with this code (switch problem)

Hi, i'm new to this forum. This is a part of a project for my university. It's in spanish and i tried to translate it to english before posting it. Anyways, i'm not asking to get it solved, i just want to know why i get an error in the switch inside the "Levels" function. The problem is that if i put more than one case i get an error and when i delete them all and just put 1 case it runs it without problems. If you could help me to get this solved or at least give me any hint on why this is not working i would be very grateful. The program i am using is Visual Studio 2013. The objective of this code is to show the levels of a game i'm to develop. thanks in advance.

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
  #include "stdafx.h"
#include <iostream>
#include <conio.h>
#include <stdio.h>

using namespace System;
using namespace std;

#define fil 20
#define col 20

void clear() {
	if (system("cls"))
		system("clear");
}

void PrintLevel(int M[fil][col])
{
	for (int i = 0;i < fil;i++)
	{
		for (int j = 0;j < col;j++)
		{
			if (M[i][j] == 0)
				cout << " ";
			else if (M[i][j] == 1)
			{
				Console::ForegroundColor = ConsoleColor::Blue;
				cout << char(219);
			}
			else if (M[i][j] == 2)
			{
				Console::ForegroundColor = ConsoleColor::White;
				cout << char(250);
			}
			else if (M[i][j] == 3)
			{
				Console::ForegroundColor = ConsoleColor::Yellow;
				cout << char(219);
			}
			else if (M[i][j] == 4)
			{
				Console::ForegroundColor = ConsoleColor::DarkYellow;
				cout << char(219);
			}
		}
		cout << endl;
	}
}

void Levels(int n)
{
	switch (n)
	{
	case 1:
		int Level1[fil][col]
			= { 
				{ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 },
				{ 1,0,0,1,2,2,2,2,2,2,2,2,2,2,2,2,1,0,0,1 },
				{ 1,0,0,1,2,1,1,1,1,1,1,1,1,1,1,2,1,0,0,1 },
				{ 1,1,1,1,2,1,1,1,1,1,1,1,1,1,1,2,1,1,1,1 },
				{ 1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1 },
				{ 1,2,1,1,2,1,1,1,0,1,1,0,1,1,1,2,1,1,2,1 },
				{ 1,2,1,1,2,1,0,0,0,0,0,0,0,0,1,2,1,1,2,1 },
				{ 1,2,1,1,2,1,0,1,1,0,0,1,1,0,1,2,1,1,2,1 },
				{ 1,2,1,1,2,0,0,1,1,0,0,1,1,0,1,2,1,1,2,1 },
				{ 1,2,1,1,3,4,3,1,1,1,1,1,1,0,3,2,1,1,2,1 },
				{ 1,2,1,1,2,0,2,1,2,2,2,2,1,0,4,0,1,1,2,1 },
				{ 1,2,1,1,2,1,2,1,2,1,1,2,1,2,3,2,1,1,2,1 },
				{ 1,2,1,1,2,1,2,1,2,1,1,2,1,2,1,2,1,1,2,1 },
				{ 1,2,1,1,2,1,2,2,2,2,2,2,2,2,1,2,1,1,2,1 },
				{ 1,2,1,1,2,1,1,1,2,1,1,2,1,1,1,2,1,1,2,1 },
				{ 1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1 },
				{ 1,1,1,1,2,1,1,1,1,1,1,1,1,1,1,2,1,1,1,1 },
				{ 1,0,0,1,2,1,1,1,1,1,1,1,1,1,1,2,1,0,0,1 },
				{ 1,0,0,1,2,2,2,2,2,2,2,2,2,2,2,2,1,0,0,1 },
				{ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 }
		};

		PrintLevel(Level1);
		break;

	case 2:
		cout << "sup";
		break;
	}
}

int main()
{
	int n;

	do
	{
		do
		{
			Console::ForegroundColor = ConsoleColor::White;
			cout << "Input level (1-5; 0 to exit): ";
			cin >> n;
		} while (n < 0 && n>5);

		Levels(n);

		_getch();
		clear();
	} while (n != 0);

	getch();
	return 0;
}
show us the error
I think you have to declare int Level1[fil][col] before the switch statement, then in your switch statement assign it.
The error code is C2360, it says that "Initialization of 'Level1' is omitted on the label 'case'".
The solution you gave me worked integralf. However, when i do that, the level1 is not printed correctly by the PrintLevel function. Thank you anyways, it helped a lot.
Last edited on
Topic archived. No new replies allowed.