• Register
3 votes
1k 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 4 views
4 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 5.6k points
0 votes
1 answer 14 views
14 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 5.1k points
0 votes
1 answer 3 views
3 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.1k points
0 votes
1 answer 934 views
934 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 7 views
7 views
Problem: I have the following error in my Calculator code and do not understand how to correct it. Please any advice would be helpful. ERROR: error: jump to case label [-fpermissive]| error:crosses initialization of 'int sum'| error: 'exit' was not declared in this scope|
asked Dec 26, 2020 Mashhoodch 5.6k points
0 votes
1 answer 6 views
6 views
How I will solve this error?
asked Dec 19, 2020 TeamScript 9.2k points
0 votes
1 answer 15 views
0 votes
1 answer 6 views
6 views
Problem: What is the difference between switch statement and if else statment ? can anyone explain with examples.
asked Dec 23, 2020 prog_learner 2.5k points
1 vote
1 answer 11 views
11 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
0 votes
1 answer 57 views
57 views
Problem: Hello! I am a new programmer and just started learning C++ about 2 weeks ago. I was working with labels and was trying to run following program when I encountered the above mentioned error: #include<iostream> #include<string> using namespace std; int main() ... what is wrong with my program due to which this error is occurring. If someone knows, kindly help me solving this error. Thanks!
asked Nov 28, 2020 Code Learner 5.1k points