如何在else-if语句中正确使用or语句(||)

时间:2017-03-02 18:51:00

标签: c++ if-statement logical-operators

考虑以下代码:

string GameExit;
bool GameChoiceGo = true;

while (GameChoiceGo == true)
{
    system("cls"); 
    cout << "\n  Are you sure you want to exit? (Yes or No)     ";
    cin >>  GameExit;
    if (GameExit == "y" || "Y" || "yes" || "Yes" || "YES")
    {
        cout << "User typed Yes";
        Sleep(3000);
        system("cls");
        break;
    }
    if (GameExit == "n" || "N" || "no" || "No" || "NO")
    {
        cout << "User typed No";
        Sleep(3000);
        system("cls");
        GameChoiceGo = false;
    }
    else
    {
        cout << "\nI'm sorry but, " << GameExit << " is not a acceptable choice. Type: Yes or No.\n\n\n";
        Sleep(3000);
        system("cls");
    }
}
break;

此处仅激活第一个语句。即使用户输入"No"或其他内容,也会输出"user typed yes"

如果我只用一个语句(即else-if statementsor statements)替换"y",则"n"可以正常工作。唯一的问题是,我希望有任何可能的yes和no版本,用户可能会输入代码。

为什么代码无法正常工作的任何想法?

4 个答案:

答案 0 :(得分:8)

对不起,但是你必须为你要检查的每个条件写GameExit ==

if (GameExit == "y" || GameExit == "Y" || GameExit == "yes" || GameExit == "Yes" || GameExit == "YES")

如果你写if ("y")(这基本上就是你正在做的事情,只有更多的陈述),const char[]将衰减为const char*,并且该指针将与0进行比较{1}}。现在,该指针将从不为空,因为将始终为字符串文字分配内存。

更好的解决方案是(1)创建一个包含所有选项的数组,以便检查条件变为简单搜索或(2)将输入转换为全例小写,并进行比较。

// 1)
std::vector<std::string> options = { "y", "Y", "yes", "Yes", "YES" };

if (std::find(options.begin(), options.end(), GameExit) != options.end());
// or
if (std::any_of(options.begin(), options.end(), [&GameExit](const auto& value) { 
        return GameExit == value;
   }); 

// 2)
std::transform(GameExit.begin(), GameExit.end(), GameExit.begin(), ::tolower);

if (GameExit == "y" || GameExit == "yes");

如果您不知道他们做了什么,您可以查找这些功能:)。

答案 1 :(得分:4)

在“您的”代码中使用OR运算符的正确方法如下(请注意==运算符之间明确使用||语句:

    if (GameExit == "y" || GameExit =="Y" || GameExit =="yes" || GameExit =="Yes" || GameExit =="YES")
    {
        cout << "User typed Yes";
        Sleep(3000);
        system("cls");
        break;
    }
    if (GameExit == "n" || GameExit =="N" || GameExit =="no" || GameExit =="No" || GameExit =="NO")
    {
        cout << "User typed No";
        Sleep(3000);
        system("cls");
        GameChoiceGo = false;
    }

PS:上述答案并非旨在提供类似情况下的最佳编程实践,而是以最小的代码更改给出OP的具体答案:)

// -----

编辑:这是使用STL的更好方法。请注意,(未排序)数组查找需要线性搜索,而unordered_set(哈希集)具有(平均)恒定时间查找。这将更快,特别是当yes,no等选项充足时。

    #include <unordered_set>

    ...
    // These sets can be as large as possible or even dynamically 
    //  updated while the program is running. insert, remove, lookup will
    // all be much faster than a simple array.
    unordered_set<string> ySet{"y", "Y", "yes", "Yes", "YES"};
    unordered_set<string> nSet{"n", "N", "no", "No", "NO"};

    if (ySet.find(GameExit) != ySet.end())
    {
        cout << "User typed Yes";
        Sleep(3000);
        system("cls");
        break;
    }
    if (nSet.find(GameExit) != nSet.end())
    {
        cout << "User typed No";
        Sleep(3000);
        system("cls");
        GameChoiceGo = false;
    }
    ...

答案 2 :(得分:3)

您需要为每个表达式定义完全相等,如下所示:

{{1}}

答案 3 :(得分:1)

GameExit == "y" || "Y" || ....

不正确。正确的方法是:

GameExit == "y" || GameExit == "Y" || ....

依此类推,无论是或不是。