我正在尝试编写一个类似游戏的冒险程序,我需要if else语句才能正常工作.但是,每当我运行if if语句时,它都不会输出我想要的内容.
#includeusing 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?:
我想在解决这个问题上提供一些帮助,因为我无法找到为什么会发生这种情况的答案.
cin >> input;
不读空格.你需要使用std::getline
.
getline(cin, input);