"如果"语句没有正确执行c ++

Use*_*000 1 c++ if-statement codeblocks

我对ifc ++中的语句有一个小问题.以下是代码片段:

string answer;
cin >> answer;

if (answer == "stay in bed")
{
   cout << "You lay there, motionless. Silent.";
}
else if (answer == "go to the bathroom")
{
   cout << "You get up and walk across the hall to the bathroom.";
}
else if (answer == "go downstairs")
{
   cout << "You get up and walk down the stairs to the kitchen.";
}
else
{
   cout << "That is not a valid answer...";
}
Run Code Online (Sandbox Code Playgroud)

当我输入任何值时,我从else语句中获得输出.如,"That is not a valid answer..."

有什么建议?我也在做这一切Code::Blocks.

Som*_*ude 7

两件事:字符串的比较取决于案例; 其次,输入操作符>>在空格上分隔,因此您不能使用它来输入多个单词,这会导致您遇到的问题.

您可能希望使用例如std::getline,一次性读取整行.