C++ Switch 语句大小写错误

发布于 2024-08-26 06:21:09 字数 5268 浏览 5 评论 0原文

我正在编写一个简单的基于文本的角色扮演游戏,使用 switch 语句进行游戏循环。该程序工作正常,直到我尝试添加另一个 case 语句,此时它给了我以下三个错误:“跳转到 case 标签”(错误发生在新添加的 case 行),以及两个“交叉初始化 ' ClassName *objectName'"(情况2中创建新对象时出现错误)。我将粘贴重要的代码,如果有人需要更多,请告诉我。

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;
    }

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 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;
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

活泼老夫 2024-09-02 06:21:09

听上去,您已经:

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

case 3:

为了到达情况 3,编译器会生成一个跳过 somevar 初始化的跳转。

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

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

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;
醉殇 2024-09-02 06:21:09

将声明包装在堆栈框架中...呃...本地范围...:)

switch(gamestate)
{
    case 0:
    {
      Apple a;
      a.DoSomething();
    }
    break;

    case 1: /* etc. */ break;
    case 2: /* etc. */ break;
}

...或者将它们移到交换机之外:

Apple A;
switch(gamestate)
{
     case 0: a.DoSomething(); 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;
最丧也最甜 2024-09-02 06:21:09

考虑以下问题:

switch (x)
{
    case 0:
        int i = 0;
    case 1:
        i = 5;
}

如果 x 是 1 怎么办?然后我们跳过i的初始化并开始使用它。这就是您得到的结果:case 3 可以访问 case 2 中的变量,但是如果您使用它们,您就已经开始使用它们而无需运行它们的初始化。

常见的解决方案是引入范围:

switch (x)
{
    case 0:
    {
        int i = 0;
    }
    case 1:
    {
        i = 5; // not possible, no i in this scope
    }
}

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
    }
}
骄傲 2024-09-02 06:21:09

编辑

现在我们看到更多的代码,问题很明显,这

        case 2:
            Weapon *wCurrent = weaponList[heroWeapon];
            Armor *aCurrent = armorList[heroArmor];

声明了两个变量,所以你不能在它后面放一个 case ,除非你将 case 2 的主体包装在下面的 {}

原始答案中


case 中声明的变量的范围是包围开关的大括号,除非您添加一组额外的大括号。所以这样的事情是有效的。

    switch(gamestate)
    {
        case 0:
            foo a;
            break;
    }

但这允许情况 1 跳过 a 的初始化但仍然引用它,因此它会生成错误。

    switch(gamestate)
    {
        case 0:
            foo a;
            break;

        case 1:
            break;
    }

所以你需要这样做,现在 a 的范围仅限于 case 0。

    switch(gamestate)
    {
        case 0:
            {
            foo a;
            }
            break;

        case 1:
            break;
    }

顺便说一句,当你编辑代码以省略不相关的内容时,你也删除了导致问题的代码。 ;)

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. ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文