我正在尝试编写一个类似游戏的冒险程序,我需要if else语句才能正常工作。但是,每当我运行if if语句时,它都不会输出我想要的内容。
#include <iostream>
using namespace std;
int main()
{
while(true)
{
cout << endl << "What will you do?: ";
string input;
cin >> input;
if (input == "Nothing" || input == "nothing")
{
cout << "Why would you do nothing?" << endl;
}
else if (input == "Look North" || input == "Look north" || input == "look North" || input == "look north")
{
cout << "You look north. There is nothing north." << endl;
}
}
}
我期望输出的是:
What will you do?: nothing
Why would you do nothing?
What will you do?: look north
You look north. There is nothing north.
What will you do?:
但是当我把这些作为输入时,我没有得到它,我得到了这个:
What will you do?: nothing
Why would you do nothing?
What will you do?: look north
What will you do?:
What will you do?:
我想帮助解决这个问题,因为我无法找到解决这个问题的原因。
答案 0 :(得分:5)
cin >> input;
不会读取空格。您需要使用std::getline
。
getline(cin, input);
答案 1 :(得分:0)
因为当你输入&#34;向北看&#34; 运用 CIN&GT;&GT;输入; 只有&#34;看&#34;保存到输入变量..
答案 2 :(得分:0)
问题在于cin
语句,因为它不处理空格,请尝试发布here。此外,您会发现有助于tolower()
功能,以规范您的输入。