我对代码来说相对较新,我尝试使用if,else if和else语句。我在下面编写了代码,但每次输入可能的输入之一时,else语句就会出现。例如,对于我写的输入,您呢?但输出是“抱歉,您输入错字了,或者我目前没有对此做出回应”,而不是“我做得不错”。
string c;
std::cin >> c;
if (c == "how about you?")
{
cout << "i'm doing decent \nthanks for asking";
}
else if (c == "How about you?")
{
cout << "i'm doing decent \nthanks for asking";
}
else if (c == "how about you")
{
cout << "i'm doing decent \nthanks for asking";
}
else if (c == "how bout you")
{
cout << "i'm doing decent \nthanks for asking";
}
else
{
cout << "Sorry, either you made a typo or I currently don't have a response to that \nthank you for your time";
}
答案 0 :(得分:3)
std :: cin一旦遇到空格,将停止读取。因此,即使您输入了“您呢?”,但只有“如何”存储在字符串c中。这就是为什么其他部分正在执行的原因。
答案 1 :(得分:3)
使用getline(cin, c)
代替reference