为什么这个案例块不执行

Why does this case block not execute?

本文关键字:执行 案例 为什么      更新时间:2023-10-16

几周前我才开始尝试C++。在尝试C++之前,我对Java有了相当不错的掌握。很多人告诉我,它们在语法意义上非常相似。

底部有一个开关语句,启动了打斗场景。每当我选择战斗选项时,它都会关闭程序。

这是我的代码:

#include "stdafx.h"
#include <iostream>
#include <cstdlib>     // For rand()
#include <string>
#include <sstream>
#include <algorithm>   // transform()
#include <cctype>      // toupper(), tolower()
#include <functional>  // ptr_fun()
#include <time.h>
// PUT S*** BELOW THIS POINT
//____________________________________________________________________________
using namespace std;
int main()
{
    /*    Expirimental text based adventure game.
     *    Mainly being used for practice methods.
     *    Developed by Zack Cook.
     Generic title. Bad story. Bad interactions. 
     Lots and lots of bad, bad code. Be warned.
     */   
    string charName;
    string charChoice;
    int charDecision;
    int playerHealth = 100;
    int randomNumber;
    int orcHealth = 100;
    srand (time(NULL));
    cout << "_____________________________________________" << endl;
    cout << "|   #####   #########     ###     ###   ### |" << endl;
    cout << "| ###   ###    ###      ### ###    ### ###  |" << endl;
    cout << "|   ###        ###     ###   ###    #####   |" << endl;
    cout << "|     ###      ###     #########     ###    |" << endl;
    cout << "| ###   ###    ###    ###     ###    ###    |" << endl;
    cout << "|   #####      ###    ###     ###    ###    |" << endl;
    cout << "_____________________________________________" << endl;
    cout << "" << endl;
    cout << "" << endl;
    cout << "Welcome player. What is your name?" << endl;
    getline(cin, charName);
    yesOrNo:
    cout << "Hello " << charName << ". Are you ready to begin?" << endl;
    getline(cin, charChoice);
    transform( charChoice.begin(), charChoice.end(), charChoice.begin(), toupper );
    cout << charChoice << endl;
    if(charChoice == "YES"){
        cout << "Good. Let's begin." << endl;
    }
    else if(charChoice == "NO"){
        system ("exit");
    }
    else
    {
        cout << "That is not a good answer." << endl;
        goto yesOrNo;
    }
    cout << "Our story begins with a wanderer named " << charName << " passing through the small town of Hark's Pass." << endl;
    cout << "A little cozy village with no more than 30 or so home stayers.nThe village men work hard on the farms during the day." << endl;
    cout << "The women cater to the children, and other house hold chores.nIn the evening, most of the village turns to The Rusty Trough for a bit of drink." << endl;
    cout << "As the sun starts to set, our wanderer, " << charName << ", starts foot towards The Rusty Trough." << endl;
    cout << "As " << charName << " enters the tavern, a heavily drunken Orc man stumbles towards the entrance." << endl;
    cout << ""I've never seen you 'round here!" The orc says to our wanderer. "I think it's time to teach these adventure types what we really think about 'em"" << endl;
    cout << "" << endl;
    cout << "What will you do?" << endl;
    cout << "1| Get ready to fight!" << endl;
    cout << "2| Call for help!" << endl;
    cout << "3| Try to run!" << endl;
    cout << "4| Do nothing at all!" << endl;
    cout << "5| Try to reason!" << endl;
    cin >> charDecision;
    switch(charDecision)
    {
    case '1': 
        do{
            cout << "FIGHT" << endl;
            randomNumber = rand() % 100 + 1;
            if(randomNumber >= 50){
                orcHealth = orcHealth - (randomNumber - (randomNumber / 5));
            cout << "You hit the orc! He now has " << orcHealth << " life left!" << endl;
            }
            else
            {
                playerHealth = playerHealth - (randomNumber - (randomNumber / 5));
                cout << "The orc hit you! You now have " << playerHealth << " life left!" << endl;
            }
        }while(playerHealth || orcHealth != 0);
        break;
    default:
        break;
    }
    return 0;
}

您的 switch 语句将int charDecision'1'进行比较,这是一个char

您从标准输入读取到int这意味着charDecision将包含 1。然后,您将此 1 与 '1' 进行比较,当转换为 int 时,转换为 49。因此,您的输入将永远不会匹配(除非您输入 49(。

修复:与1进行比较或charDecision成为char

变量charDecision声明为int

C++标准 I/O 流类(包括 cincout 等(相对智能,并且根据您提供给它的变量类型"做正确的事"。

执行表达式 cin >> charDecision 时,cin 读取看起来像数字的所有内容,并将其转换为数字的本机表示形式。因此,当您在该菜单上键入1时,存储的是文字数字 1。您的交换机正在测试文字字符 '1' ,其整数值为 49,因此它不匹配,因为 49 != 1。

您需要将charDecision类型更改为char或测试数字1而不是字符'1'

while (playerHealth != 0 || orcHealth != 0)我认为这就是问题所在。

您的主要问题是使用goto,摆脱它!

yesOrNo:
cout << "Hello " << charName << ". Are you ready to begin?" << endl;
getline(cin, charChoice);
transform( charChoice.begin(), charChoice.end(), charChoice.begin(), toupper );
cout << charChoice << endl;
if(charChoice == "YES"){
    cout << "Good. Let's begin." << endl;
}
else if(charChoice == "NO"){
    system ("exit");
}
else
{
    cout << "That is not a good answer." << endl;
    goto yesOrNo;
}

可以替换为

do
{
    cout << "Hello " << charName << ". Are you ready to begin?" << endl;
    getline(cin, charChoice);
    transform( charChoice.begin(), charChoice.end(), charChoice.begin(), toupper );
    cout << charChoice << endl;
    if(charChoice == "YES"){
        cout << "Good. Let's begin." << endl;
        startGame();//it is the function where you should place your game logic, the "switch" block in your case
        //if you want to get back to main menu after the end of the game you can delete next line:
        break;//leave this loop
    }
    else if(charChoice == "NO"){
        break;//leave this loop
    }
    else
    {
        cout << "That is not a good answer." << endl;
    }
} while (true);

同样这样做,您可以在游戏结束后返回主菜单。
我还建议你将游戏逻辑拆分为不同的函数,代码将更容易编写和维护。另外,我建议您在完成后以面向对象的方式重写游戏,这对您来说可能是一个很好的做法。忘记goto,它可以将任何代码变成一段废话。