C&C++   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了C Switch语句错误大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用一个游戏循环的switch语句编写一个简单的基于文本的RPG.该程序正常工作,直到我尝试添加一个case语句,此时它给出了以下三个错误:“跳转到案例标签”(错误发生在新添加的案例的行),以及两个“十字架初始化” ClassName * objectName’“(在案例2中创建新对象时发生错误).我会粘贴重要的代码,如果有人需要更多,请告诉我.

int main(void)
{
    // add weapons to array
    Weapon *weaponList[12];
    // Rusty Sword
    weaponList[0] = new Weapon(0,0);
    weaponList[0]->SetAll(0,2,3);
    // Bronze Sword
    weaponList[1] = new Weapon(0,0);
    weaponList[1]->SetAll(1,5,10);
    // Bronze Battle Axe
    weaponList[2] = new Weapon(0,0);
    weaponList[2]->SetAll(2,15,30);
    // Iron Sword
    weaponList[3] = new Weapon(0,0);
    weaponList[3]->SetAll(3,25,70);

    // add armor to array
    Armor *armorList[12];
    // Worn Platemail
    armorList[0] = new Armor(0,0);
    armorList[0]->SetAll(0,3);
    // Bronze Chainmail
    armorList[1] = new Armor(0,0);
    armorList[1]->SetAll(1,8);
    // Bronze Platemail
    armorList[2] = new Armor(0,0);
    armorList[2]->SetAll(2,7,20);
    // Iron Chainmail
    armorList[3] = new Armor(0,0);
    armorList[3]->SetAll(3,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;
    }

解决方法

通过它的声音,你有:

case 2:
    Type somevar = ...;
    ...
    break;

case 3:

为了达到案例3,编译器会在somevar的初始化之后@L_674_14@跳转.

要修复,请使用大括号创建围绕变量声明的块:

case 2:
    {
    Type somevar = ...;
    ...
    }
    break;

大佬总结

以上是大佬教程为你收集整理的C Switch语句错误全部内容,希望文章能够帮你解决C Switch语句错误所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。