C++ 我的if语句打印在while循环中的每个if语句上,不管它是否为false

C++ 我的if语句打印在while循环中的每个if语句上,不管它是否为false,c++,if-statement,C++,If Statement,所以我在做一个文字冒险游戏,我只是在编程说明。我有一个南北等的脚本,然后是一个if语句,如果用户输入的不是方向,它会说不是方向,循环到顶部,但它不工作。即使我输入了正确的输入,它也将始终打印非方向。有人能帮忙吗 #include <iomanip> #include <string> #include <iostream> using namespace std; int main() { string input; while (inp

所以我在做一个文字冒险游戏,我只是在编程说明。我有一个南北等的脚本,然后是一个if语句,如果用户输入的不是方向,它会说不是方向,循环到顶部,但它不工作。即使我输入了正确的输入,它也将始终打印非方向。有人能帮忙吗

#include <iomanip>
#include <string>
#include <iostream>
using namespace std;



int main()
{
    string input;
    while (input != "north", "n", "south", "s", "east", "e") {
        cout << "Enter a direction" << endl;
        getline(cin, input);
        if (input == "north" || input == "n") {
            cout << "north" << endl;
        }
        if (input == "west" || input == "w") {
            cout << "west" << endl;
        }
        if (input == "east" || input == "e") {
            cout << "east" << endl;
        }
        if (input == "south" || input == "s") {
            cout << "south" << endl;
        }
        if (input != "n", "s", "e", "w")
        {
            cout << "That is not a direction" << endl;
        }
    }
    return 0;
}
#包括
#包括
#包括
使用名称空间std;
int main()
{
字符串输入;
而(输入!=“北”、“北”、“南”、“南”、“东”、“东”、“东”){

正如我在评论中所说:

在C++中,<代码>“北方”、“n”、“南方”、“s”、“东”、“e”< /代码>将始终被评估为<代码>“e”/>代码>和<代码>“n”、“s”、“e”、“w”< /> >将始终被评估为<代码>“w”< /> >

进行这种比较的正确方法(对于所有可能的情况)是:

如果是的话:

if (input != "n" || input != "s" || input != "e" || input != "w") {...}
intmain()
{
字符串输入;
while(true){

CUTE不能只做你自己的语法来做事情。<代码>“No”,“s”,“e”,<代码>输入,“=”,“N”,“S”,“E”,“W”< /代码>不要做任何有用的事。在C++中,“代码”>“北方”,“N”,“南方”,“S”,“东”,“E”< /代码>将永远被评估为<代码>“E”/> >和>代码>“N”,“S”,“E”,“W”。
将始终计算为
“w”
为什么在
之后捕获用户输入,而
?您的程序从未获得初始条件satisfied@Inian,初始条件将得到满足,因为字符串为空,因此与初始条件正在检查的任何接受值都不匹配。如果执行一段时间,则会更有用ful tbhAlready在这里发帖:是的,我刚刚试过,但没用,它仍然打印出来,这不是一个direction@Jacecraft29您需要将if语句的其余部分更改为elseif@Jacecraft29实际上,看到我的新编辑,while条件是错误的,我做了所有的事情,现在它只是说,当我进入北部和北部时,这不是一个方向n它结束了program@Jacecraft29请看演示。我现在猜不出你的程序是什么样子的,但是如果它在演示中不起作用,请在这里说什么输入不起作用。这也起作用了。谢谢,我的while现在可以小得多。为什么你在最后一个
if
中有一个
中断
是的,我的错误在这里。
if (input != "n" || input != "s" || input != "e" || input != "w") {...}
int main()
{
    string input;
    while (true) {
        cout << "Enter a direction" << endl;
        getline(cin, input);
        if (input == "north" || input == "n") {
            cout << "north" << endl; break;
        }
        else if (input == "west" || input == "w") {
            cout << "west" << endl; break;
        }
        else if (input == "east" || input == "e") {
            cout << "east" << endl; break;
        }
        else if (input == "south" || input == "s") {
            cout << "south" << endl; break;
        }
        else {
            cout << "That is not a direction" << endl;
        }
    }
    return 0;
}