Advertisement

Variables within a switch() statement

Started by February 15, 2002 09:14 AM
3 comments, last by Fundy Glostna 22 years, 10 months ago
My compiler, MVC++ 6.0, always sends extremely annoying error messages when I create a variable within a certain case label of a switch() statement. The error reads "Initializing of variable WHATEVER skipped by default/case label." I seem to have to initialize all my variables before entering the switch(), even if I only need them for one label. Does anyone know how to get around this? example:
  
switch(battle_state)
{
case BATTLE_SETUP:
    int enemycount;
    //this would cause the said error message

    //I would have to create enemycount outside the switch()

break;
default:break;
}
  
No, Canadians DON'T all live in igloos. They live in one BIG igloo!
Yeah, do this:
      switch(battle_state){case BATTLE_SETUP:  {    int enemycount;  } break;default:break;}  
Advertisement
OK, but will those variables be lost after the closing "}" bracket, like in a function?
No, Canadians DON'T all live in igloos. They live in one BIG igloo!
They leave scope upon reaching the ''}'' so yes.

"Don''t be afraid to dream, for out of such fragile things come miracles."
Don't let that dissuade you from following the advice. It would have gone out of scope after the switch anyway. Incidently, that's one reason a clear and consistent indention standard is a good programming practice. Scope is easier to decipher.

ShadeStorm, the Day_Glo Fish

Edited by - ShadeStorm on February 16, 2002 8:43:49 AM
ShadeStorm, the Day_Glo Fish

This topic is closed to new replies.

Advertisement