C++ Switch Statement Case Error
I'm programming a simple text-based RPG using a switch statement for a game loop. The program works fine until I attempt to add another case statement, at which point it开发者_如何学Python gives me the following three errors: "jump to case label" (error occurs at the line of the newly added case), and two "crosses initialization of 'ClassName *objectName'"(errors occur when the new objects are created in case 2). I'll paste the important code, if anyone needs more, please let me know.
int main(void)
{
// add weapons to array
Weapon *weaponList[12];
// Rusty Sword
weaponList[0] = new Weapon(0,0,0);
weaponList[0]->SetAll(0,2,3);
// Bronze Sword
weaponList[1] = new Weapon(0,0,0);
weaponList[1]->SetAll(1,5,10);
// Bronze Battle Axe
weaponList[2] = new Weapon(0,0,0);
weaponList[2]->SetAll(2,15,30);
// Iron Sword
weaponList[3] = new Weapon(0,0,0);
weaponList[3]->SetAll(3,25,70);
// add armor to array
Armor *armorList[12];
// Worn Platemail
armorList[0] = new Armor(0,0,0);
armorList[0]->SetAll(0,2,3);
// Bronze Chainmail
armorList[1] = new Armor(0,0,0);
armorList[1]->SetAll(1,5,8);
// Bronze Platemail
armorList[2] = new Armor(0,0,0);
armorList[2]->SetAll(2,7,20);
// Iron Chainmail
armorList[3] = new Armor(0,0,0);
armorList[3]->SetAll(3,15,60);
while(gamestate != 8)
{
switch(gamestate)
{
case 0:
cout << " /| Welcome!\n"
<< " || \n"
<< " || \n"
<< " || \n"
<< "_||_ \n"
<< " 88 \n"
<< " 88 Name: ";
cin >> heroName;
gamestate = GAME_STATE_MENU;
break;
case 1:
cout << "\n"
<< "'/stats' will show you your stats\n"
<< "'/shop' will let you visit the weapon shop\n"
<< "secret commands: /setweapon # /setarmor # /setheroexp #\n"
<< "\n";
cout << "Command: ";
cin >> command;
if (strcmp(command, "/stats") == 0)
{
gamestate = 2;
break;
}
else if (strcmp(command, "/shop") == 0)
{
gamestate = 3;
break;
}
else if (strcmp(command, "/fight") == 0)
{
gamestate = 4;
break;
}
else if (strcmp(command, "/setweapon") == 0)
{
cin >> testNum;
heroWeapon = testNum;
break;
}
else if (strcmp(command, "/setarmor") == 0)
{
cin >> testNum;
heroArmor = testNum;
break;
}
else if (strcmp(command, "/setheroexp") == 0)
{
cin >> testNum;
heroExp = testNum;
LevelUp();
break;
}
else if (strcmp(command, "/exit") == 0)
{
gamestate = 8;
break;
}
else
{
cout << "Please enter a valid command.\n";
gamestate = 2;
break;
}
case 2:
Weapon *wCurrent = weaponList[heroWeapon];
Armor *aCurrent = armorList[heroArmor];
heroWeaponPower = wCurrent->GetWeaponAttack();
heroArmorDefense = aCurrent->GetArmorDefense();
heroPowerDefault = ((heroLevel - 1) * 10) + 10;
heroPower = heroPowerDefault + (heroStrength * 2) + heroWeaponPower;
heroDefenseDefault = ((heroLevel - 1) * 2) + 5;
heroDefense = heroDefenseDefault + (heroAgility / 5) + heroArmorDefense;
heroHealthDefault = (heroLevel * 5) + 20;
heroHealth = heroHealthDefault + (heroStamina * 10);
cout << "\nS T A T S\nName: "
<< heroName
<< "\nLevel: "
<< heroLevel
<< "\nExp: "
<< heroExp << "/" << expForLevel[heroLevel]
<< "\nGold: "
<< heroGold
<< "\nHealth: "
<< heroHealth
<< "\nPower: "
<< heroPower
<< "\nDefense: "
<< heroDefense
<< "\nWeapon: "
<< weaponNameList[heroWeapon]
<< "\nArmor: "
<< armorNameList[heroArmor]
<< "\n\n";
system("PAUSE");
gamestate = 2;
break;
case 3:
break;
}
}
return 0;
}
By the sounds of it, you have:
case 2:
Type somevar = ...;
...
break;
case 3:
To reach case 3, the compiler generates a jump past the initialization of somevar
.
To fix, use braces to create a block surrounding the variable declaration:
case 2:
{
Type somevar = ...;
...
}
break;
Wrap declarations in a stack frame...er...local scope... :)
switch(gamestate)
{
case 0:
{
Apple a;
a.DoSomething();
}
break;
case 1: /* etc. */ break;
case 2: /* etc. */ break;
}
...or move them outside the switch:
Apple A;
switch(gamestate)
{
case 0: a.DoSomething(); break;
Consider the following:
switch (x)
{
case 0:
int i = 0;
case 1:
i = 5;
}
What if x
is 1? Then we skip over the initialization of i
and just start using it. This is what you're getting: case 3
has access to variables from case 2
, but if you use them you've started using them without running their initialization.
The common solution is to introduce scope:
switch (x)
{
case 0:
{
int i = 0;
}
case 1:
{
i = 5; // not possible, no i in this scope
}
}
edit
Now that we see more code, the problem is obvious, this
case 2:
Weapon *wCurrent = weaponList[heroWeapon];
Armor *aCurrent = armorList[heroArmor];
declares two variables so you can't put a case after it unless you wrap the body of case 2 in {}
original answer below
The scope of variables declared in a case are the braces that enclose the switch unless you add an extra set of braces. so something like this works.
switch(gamestate)
{
case 0:
foo a;
break;
}
but this allows case 1 to skip initialization of a but still reference it, so it generates an error.
switch(gamestate)
{
case 0:
foo a;
break;
case 1:
break;
}
So you need to do this instead, now the scope of a is limited to case 0.
switch(gamestate)
{
case 0:
{
foo a;
}
break;
case 1:
break;
}
Incidently, when you edited your code to leave out the irrelevant stuff, you also removed the code that caused the problem. ;)
精彩评论