"jumping the case label"?

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?

tx

-Roy

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;

thanks. that makes sense. The "jumping the case label" msg didn't

Hi,
I've got the same problem, and tried adding curly brackets as given below, but I still get the error. Does anyone know why?

case 1:
{
int ledLevel = random(0,ledCount);
break;
}

Post your entire Sketch. Please use code tags.

I've got the same problem, and tried adding curly brackets as given below, but I still get the error. Does anyone know why?

Looking just at the code snippet you posted, I would guess that you do not have the same problem. You have a different, but similar, problem.

As Coding Badly suggests, you need to post more of your code, and the EXACT error message.