c++ - 我可以在 if 语句中与多个字符串进行比较吗?

标签 c++

这会被 mock ,因为我可能非常愚蠢,但我可以在 if 语句中使用多个字符串作为条件吗?

#pragma once
#include <iostream>
#include <string>
#include "Baxter.h"
#include "Inventory.h"
#include "Room1Items.h"


int woman = 6;
int children = 5;

int inputNumberOfAnimalsToSave;
int numberOfAnimals;
int numberOfAnimalsToKill;
int numberOfAnimalToTakeHome;
std::string Yes;
std::string firstAction;

bool killRemainingAnimals;
int playerSaidYes;

int AddNumber()
{
    numberOfAnimals = woman + children;
    return numberOfAnimals;
}

int RemoveNumber() 
{
    numberOfAnimalsToKill = numberOfAnimalToTakeHome - numberOfAnimals;

    return numberOfAnimalsToKill;
}

int InputNumber() 
{

    std::cout << "   Comrade Kurchenko:  Well, they are irridiated and will most likely end up poisioning \n";
    std::cout << "     your family, but sure, why not! How many animals Shall we Save ?\n ";
    std::cin >> inputNumberOfAnimalsToSave;
    numberOfAnimalToTakeHome = numberOfAnimals - inputNumberOfAnimalsToSave;

    return numberOfAnimalToTakeHome;
}
int DoYouWantToKillTheRest() 
{

        std::cout << "   Comrade Kurchenko:   Fair Enough Comrade! Do you want to move on? \n\n";
        std::cout << "   Private Lenin:  "; std::cin >> Yes;

    while (Yes == "No") 
    {

            //std::cout << "   Comrade Kurchenko:  So, you want the remaining " << numberOfAnimalToTakeHome << " Put The sleep do you?\n\n";
            //std::cout << "   Private Lenin:  Im afraid so sir!\n\n";

            //std::cout << "   Comrade Kurchenko:  Then so be it. They will be better off dead by our hands, than starving to death.\n\n\n\n";
            //std::cout << "       *** Loud Bangs and Then Silence....\n\n\n ***";


        std::cout << "   Comrade Kurchenko:  What do you want to do?\n";
        std::cout << "   Private Lenin:  "; std::cin >> firstAction; std::cout << "\n";

        while (firstAction != "MoveOn")
        {



            if (firstAction == "Take food" || "Recover Meal" )
            {
                if (canTakeFood) 
                {
                    TakeFood();
                    std::cout << "    You have taken a peice of food \n";
                    DoYouWantToKillTheRest();
                }
                if (canTakeFood == false) 
                {
                    std::cout << "   There is no more food to take \n";
                    DoYouWantToKillTheRest();
                }

            }

            if (firstAction == "Eatfood")
            {
                EatFood();
                DoYouWantToKillTheRest();

            }

            if (firstAction == "Inventory")
            {
                ShowInventory();
                DoYouWantToKillTheRest();
            }

            if (firstAction == "Ouch") 
            {
                JabAFingerInYourEye();
                std::cout << "   Comrade Kurchenko : Why the hell did you stab yourself in the eye?\n\n";
                std::cout << "   Private Lenin : I dont know sir, its like someone was controlling my arm!\n";

                DoYouWantToKillTheRest();
            }

            if (firstAction == "Look")
            {
                Look();
                DoYouWantToKillTheRest();
            }

            if( firstAction == "Help")
            {
                WhatCanIDo();
                DoYouWantToKillTheRest();
            }
            if (firstAction == "Baxter")
            {
                ShowBaxter();
                std::cout << "Here is baxter";
                DoYouWantToKillTheRest();
            }

        }
            return 0;
    }
    return 0;
}

我试过了,运行时没有发现任何错误。它只是行不通。 我试图用谷歌搜索它,但我似乎无法找到正确的措辞来获得结果。玩弄基于控制台的文字冒险。

我用谷歌搜索了许多不同的方式来问这个问题,但没有得到任何对我有帮助的结果。

我没有收到任何错误消息。它运行良好,只是不起作用。

最佳答案

“我可以在一个 if 语句中比较多个字符串吗?” - 当然可以(我假设我们在这里谈论的是 std::string)。

你在做什么

if (firstAction == "Take food")

如果您想针对两个字符串进行测试,您可以这样做:

if (firstAction == "Take food" or firstAction == "Drop food")

您可以将 更改为更常规的 || ,但两者都是有效的并且做同样的事情。

关于c++ - 我可以在 if 语句中与多个字符串进行比较吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57224693/

相关文章:

c++ - 使用 OpenCV 写入 16 位未压缩图像

c++ - pimpl 习语和构建器模式之间有重叠吗?

c++ - 散列<std::string> 与散列<std::string_view>

c++ - GetTickCount() 换行时会发生什么?

c++ - 从 C++ 为 Batch 设置临时环境变量

c++ - QWidget 监听其他 QWidgets 事件

c++ - 错误 : "undefined reference to" while compiling c++

c++ - 特定目标文件的单线程构建

c++ - 正确使用c++命名空间

C++ 段错误 : 11