Hi - small question here: I was working on a project yesterday where I was using a switch/case statement and attempting to define a local var in several of the cases - something like
case: 1
// do some stuff
break;
case 2:
int p = resultOfFunctionCall();
//do something with p
break;
case 3:
int p = resultOfOtherFunctionCall();
//do something else with p
I was getting a compile error something like "jumping the case label" related to my local var. I resolved it by declaring the var inside the switch but outside any case. I am not returning the var at the end of the switch or anything like that so I wondered why the compiler would complain.
Is there some rule about not declaring local vars in a case statement?
In C and C++, variables must have unique names within the same code block; code blocks are statements enclosed by curly braces . You could add braces around your code as follows:
case 2:
{ int p = resultOfFunctionCall();
//do something with p
}
break;