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

时间:2019-07-26 17:33:35

标签: c++

这会被嘲笑,因为我可能非常愚蠢,但是我可以在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;
}

我已经尝试过,并且在运行时没有错误。就是行不通。 我已经尝试过搜索它,但似乎找不到正确的措词来获得结果。体验基于控制台的文字冒险。

我已经用多种方法查询了这个问题,但没有任何结果对我有帮助。

我没有收到任何错误消息。它运行正常,只是行不通。

4 个答案:

答案 0 :(得分:4)

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

你在做

if (firstAction == "Take food")

如果您要针对两个字符串进行测试,您可以 进行操作:

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

您可以将or更改为||,这是比较传统的做法,但是两者都是有效的,并且执行相同的操作。

答案 1 :(得分:3)

在C ++(和大多数其他编程语言)中,由于定义了运算符优先级的方式,通常无法在一个操作中将一件事(字符串变量)与多个其他事物进行比较:

// Does not work!!
if (firstAction == "Take food" || "Recover Meal" )
// Because it will evaluate to (firstAction == "Take food") || "Recover Meal"

相反,您可以使用逻辑运算符将一个比较的结果与另一个比较的结果相结合:

if (firstAction == "Take food" || firstAction == "Recover Meal")
{

您应该阅读C ++中的逻辑运算符以了解更多信息,例如:https://www.learncpp.com/cpp-tutorial/36-logical-operators/

如果您想动态比较整个字符串列表,那么当然也可以这样做:

std::set<std::string> validActions = {"Take food", "Recover meal"};
if (validActions.find(firstAction) != validActions.end())
{
  // ... found a valid action ...

答案 2 :(得分:0)

这不是在C ++中比较c字符串的方式。该类型的字符串只是一个字符数组,并且运算符==会告诉它是否是同一数组,而不是它是否具有相同的内容。 要正确比较字符串,请使用<cstring>中的函数strcmp

firstAction == "Take food"; // bad
strcmp(firstAction, "Take food") == 0; // good

更好的解决方案是使用类std::string,该类允许使用常规运算符。

std::string(firstAction) == "Take food"; // good
std::string(firstAction) == std::string("Take food"); // also good
firstAction == std::string("Take food"); // also good

或者,就像@JVApen暗示的那样,也许更好的解决方案是使用std::string_view

firstAction == std::string_view("Take food"); // good
firstAction == "Take food"sv; // the same meaning as above but written shorter

您只需要记住在此之前插入行using std::operator""sv;。 (using namespace std;也可以。)


如果您正确比较了字符串(比如使用std::string_view),那么您当然可以使用||(或)或&&(以及)或任何其他运算符进行多次比较在一个if中。

if (firstAction == "Take food"sv || firstAction == "Take water"sv)
{
    // do stuff
}
if (firstAction == "Take food"sv && !canTakeFood)
    std::cout << "   There is no more food to take \n";
    LoopAndDoAction();
}

(此代码可能没有任何意义,但这只是一个示例。)


根据您要执行的操作,考虑使用enum而不是字符串常量。枚举更快,更易读,占用更少的内存,并且在使用时像类型一样更容易出错。实际上,只有当我不想使用枚举时,我才能想到的情况就是解析用户输入。

答案 3 :(得分:-1)

第二个if是与谁可以比较字符串的字符串,如果您发现这些条件是否为true,则整个条件为true。

我在代码RecyclerView上大声疾呼

文档: http://www.cplusplus.com/reference/string/string/compare/

//comments

输出量: https://onlinegdb.com/By39WpOGS