switch statement

I am trying to create different data structures based on what the user inputs, I am wanting to use a switch to determine it. But the compiler is giving errors, I am not sure If i just cant create structures like this or if I am missing something obvious in my switch. The compiler error is saying "initialisation of 's' is skipped by 'default' label. I think that means in the case of the default case, s will not be initialised, which I am fine with, so can I not do it this way, or have i created something wrong in my switch statement.

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
  int main()
{
	int i;
	
	std::cout << "Enter value for data structure ";
	std::cin >> i;
	switch (i) 
	{
		case 1:
			stack s(5);
			s.push(2);
			s.print();
			s.push(4);
			s.print();
			s.push(6);
			s.print();
			s.push(7);
			s.print();
			s.push(10);
			s.print();

			s.pop();
			s.print();
			s.pop();
			s.print();
			s.pop();
			s.print();
			s.pop();
			s.print();
			s.pop();
			s.print();
			break;

		default:
		std::cout << "Default\n";
		break;

	}

	
}
It's because technically your stack s (in the case 1) is in the same scope as your default case, so you could access s without having initialized it.
To prevent this error, you can wrap the case in an extra pair of braces.

1
2
3
4
5
6
7
8
9
10
11
switch (i)
{
    case 1:
    {
        stack s(5);
        // ...
        break;
    }
    default:
    // ...
}
Last edited on
With only two case blocks it might be better to go if/else instead. A switch works better when there are multiple possible blocks of code to choose which one(s) to execute.

Another way to shut the compiler up other than making a case block explicit with braces is move the stack variable's initialization to outside the switch.

1
2
3
4
5
6
7
8
9
10
11
12
13
stack s(5); // initialize here so the variable is within all the case blocks

switch (i)
{
    case 1:
    {
        // ...
        break;
    }

    default:
    // ...
}

https://www.learncpp.com/cpp-tutorial/switch-statement-basics/
Last edited on
assuming this is part of a bigger question,

something like
switch
{
case 1:
stack s; blah blah..
case 2:
vector<something> s;
blah blah.
case 3:... etc
}

then you may be asking about a variant or a union (union is a cruder older way). You can also dive off into void pointers (make a little class and stuff a type in it so it knows what it really is?) or handcook something (you can stuff anything into a byte array, if you so desire, including its type and/or size up front pascal string style). But the variant is the way to do this idea, the others work but are a bit barbaric. You can also do it with templates but that may be overkill(?).
Last edited on
Thank you all for the replies. The switch will contain more cases when I get further into it, it will not just be the one case.

Another way to shut the compiler up other than making a case block explicit with braces is move the stack variable's initialization to outside the switch.


I had tried that, but the complier was giving me an error ( I have tried it again since your suggestion and it is working now - think there is a problem with my vs and it randomly throws errors for no reason)

Anyway I just used Gandos method and put some extra braces around it, and that solved my issue, so thank you Gando
Hello DonnaPin,

I managed this to get the code to work:
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
int main()
{
    int i{};

    std::cout << "Enter value for data structure ";
    std::cin >> i;

    std::cout << '\n';

    switch (i)
    {
        case 1:
        {
            std::stack<int> s;

            s.push(2);
            std::cout << s.top() << '\n';

            s.push(4);
            std::cout << s.top() << '\n';

            s.push(6);
            std::cout << s.top() << '\n';

            s.push(7);
            std::cout << s.top() << '\n';

            s.push(10);
            std::cout << s.top() << "\n\n";

            std::cout << s.top() << '\n';

            s.pop();
            std::cout << s.top() << '\n';

            s.pop();
            std::cout << s.top() << '\n';

            s.pop();
            std::cout << s.top() << '\n';

            s.pop();
            std::cout << s.top() << '\n';

            s.pop();
            break;
        }
        default:
            std::cout << "Default\n";
            break;
    }

}

in line 14 you define a stack, but do not give it its template type. Also there is not ctor that I know of that allows defining an initial size of the stack. Unlike what you can do with a vector.

I was also unable to find any "print" function for a stack. Actually I have never heard of a "print" function for a stack. Unless there is change in the 2020 standard or the next standard.

The above code produces this output:

Enter value for data structure 1

2
4
6
7
10

10
7
6
4
2


 Press Enter to continue:



Andy
Topic archived. No new replies allowed.