• Register
3 votes
1.4k views

Problem:

Getting an error while I was working with a switch statement in c++. I do have a 200 lines code that I can’t share with you for obvious reason. I am sharing the switch-statement block that produces the error in my program. The error I am getting is: cannot jump from switch statement to this case label c++

switch(foo) {
  case 1:
    int i = 42;
    dostuff(i);
    break;
  case 2:
    dostuff(i*2); 
}

Any possible solution will be admitted with thanks.

 

12 7 7
15,250 points

Please log in or register to answer this question.

2 Answers

3 votes

Solution:

You need to understand the characteristics of the switch-statement very clearly. 

In switch-statement, every case checks the individual and original condition. For every false statement, it moves forward and for a true condition, it stops checking and break the loop.

The problem occurred when you used the uninitialized variable in your second case within the same block. You need to initialize that variable in every case or make another block for the second case. Like this:

switch(foo) {
  case 1:
    {
        int i = 42; 
        dostuff(i);
        break;
    }
  case 2:
    dostuff(123);
}

I hope I am able to clear the things to you.
Thanks.

13 9 6
94,240 points
0 votes

This error can be solved by two methods;

Solution 1:

Reason:

The problem is that the variable is declared in one case and is still visible in the subsequent cases unless an explicit {} block is used, but they cannot be initialized because the initialization code belongs to another case.

Example:

In the following code, if the foo equals 1 everything will be Ok but if it is equal to 2, we will accidentally use the I variable which does not exist but probably create garbage.

switch(foo)

 {

 case 1:

 int i = 42; // i exists all the way to the end of the switch

dostuff(i);

break;

 case 2:

 dostuff(i*2); // i is *also* in scope here, but is not initialized!

}

Wrapping the case in an explicit solves the problem.

switch(foo)

 {

case 1:

 {

 int i = 42; // i only exists within the { }

dostuff(i);

 break;

 }

case 2:

dostuff(123); // Now you cannot use i accidentally

}

Solution 2:

Declaration of new variables in the case statement is what causes problems. Enclose all case statements in curly braces will limit the scope of newly declared variables to the currently executing case which solves the problem.

Example:

switch(choice)

 {

case 1:

 { // ....... }

break;

case 2:

{ // ....... }

break;

case 3:

 { // ....... }

break;

 }

 

11 5 2
3,890 points

Related questions

0 votes
1 answer 9 views
9 views
Problem: Any possible solution will be admitted with thanks: cannot jump from switch statement to this case label c++ .
asked Mar 14 Wafa Abu Yousef 6.1k points
0 votes
1 answer 16 views
16 views
Problem: The problem is that variables declared in one case are still visible in the subsequent cases unless an explicit { } block is used, but they will not be initialized because the initialization code belongs to another case.
asked Dec 15, 2020 Mashhoodch 13k points
0 votes
1 answer 93 views
93 views
Problem: Hello! I am a new learner. I recently started programming in C#. I learned the control statements in which I came across if, if-else, and switch statements. I tried the following program to have a better understanding of switch statement: using System; class HelloWorld ... label `case default:' I do not know what I have done wrong. Can anybody help me in figuring out what is wrong here?
asked Jan 4 Code Learner 9.2k points
0 votes
1 answer 55 views
55 views
Problem Hey I Am facing an error in my calculator code. I don&rsquo;t know how to resolve this error. Will you please help me to resolve this or any other help? Here&rsquo;s my code Program: #include <iostream> #include <cmath> using namespace std;          void display_menu();  int get_menu_choice(); void ... b)   {    return (a + b);   }  int subtract(int a, int b)   {     return (a - b);   }
asked Dec 21, 2020 chris jordan 2.4k points
0 votes
1 answer 1.4K views
1.4K views
Problem : I am much more familiar with the enums in C# but it seems like enums in the Java is a quite different. Currently I am trying to use the switch statement @ enums in my below code but I always face the below error no matter what I am doing. The error I ... myenumExample //... switch (myenumExample) { case MyAnotherClass.MyEnum.MYVALUE_A: { <-- facing error on this line //.. break; } } } }
asked Dec 25, 2019 alecxe 7.5k points
0 votes
1 answer 2 views
2 views
Problem: I will request someone to answer my question Please, I have searched on the web but I got no useful information for this, now I am in difficulty continuing my learning
asked Mar 24 rubelkhan 20.4k points
0 votes
1 answer 4 views
4 views
Problem: i need fix this problem who know about python please help how to use switch case in python
asked Mar 16 Rohit kr 15k points
0 votes
1 answer 1 view
1 view
Problem: I am Making a calculator using a switch case. The result I get : Enter a number : 5 Enter another number : 4 What do you want to perform on these numbers? You have entered a wrong action, please try again
asked Mar 23 ummesalma 22.8k points
0 votes
1 answer 12 views
12 views
How I will solve this error?
asked Dec 19, 2020 TeamScript 13.5k points
1 vote
1 answer 15 views
15 views
Problem: Hello Kodlogs, I am feeling good to find a helpful website like this. So many nice, and friendly people are here. Let me come to the point and put my problem here. I am new to java programming and trying to learn new things every day. I just found a new ... "Snowy"; break; case 5: dayString = "Cloudy"; break; } System.out.println(dayString); } } Please shed some light on this. Thanks.
asked Mar 26, 2020 Gavin 15.3k points