• Register
3 votes
599 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

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 1
3,890 points

Related questions

0 votes
1 answer 623 views
623 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
1 vote
1 answer 9 views
9 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 Gavin 15.3k points
0 votes
1 answer 15 views
15 views
Problem: Hello guys, I am new in the c++ although I know the basics of c but I am trying to implement is that takin the string from the user and converting it to the lower case, I don't know how to code this I have few things with ASCII value and stuff still, I am not able to get it, I am waiting for the solution of this problem.
asked Jun 21 Gavin 15.3k points
0 votes
1 answer 9 views
9 views
Problem: I am not very expert in C++.The below assert causes the Valgrind error: int* g () { int j = 2; return &j; } int main () { using namespace std; int v = *g(); // assert(v == 2); // Valgrind: Conditional jump or move depends on ... is not allowing me to call the assert on that statement. My variable 'v' has been initialized before the assertion then why is it showing me that error?
asked Aug 25 Raphael Pacheco 4.9k points