说出你的代码是:
cout << "You wake up in a room. There is a small lit candle and a door. What do you do?" << endl;
string startout;
getline(cin, startout);
if (startout == "Door" || startout == "DOOR" || startout == "door" || startout == "Open Door" || startout == "open door" || startout == "OPEN DOOR" || startout == "Open door" || startout == "Open the door" || startout == "open the door") {
cout << " " << endl;
cout << "You move to the door but its too dark to see anything besides the outline of it." << endl;
} else if (startout == "Candle" || startout == "CANDLE" || startout == "candle" || startout == "Pick Up Candle" || startout == "PICK UP CANDLE" || startout == "pick up candle" || startout == "Pick up candle") {
cout << "You pick up the candle then move to the door. With the light from the candle you can see the door well. What do you do?" << endl;
};
如果使用代码的人除了&#34; Door&#34;之外的其他东西。或&#34;蜡烛&#34; (或任何变体),代码将重新启动if语句,以便重新询问第一个问题?
Ex:用户输入:&#34;跳舞&#34;
输出:&#34;我不明白&#34;跳舞&#34;。 你做了什么?&#34;
或类似的东西。
答案 0 :(得分:1)
首先,你需要一个更好的解析器。至少,把 表格或某种地图中的合法值,带有指针 行动。然后将输入包装在函数中:
typedef void (* ActionPointer)(); // Or whatever you need.
typedef std::map< std::string, ActionPointer, CaseInsensitiveCmp >
ActionMap;
ActionPointer
getAction( ActionMap const& legalValues )
{
std::string line;
if ( ! std::getline( std::cin ) ) {
// Error on std::cin... Probably fatal.
}
ActionMap::const_iterator action = legalValues.find();
return action == legalValues.end()
? nullptr
: action->second;
}
然后你可以这样写:
std::cout << "You wake up in a room. There is a small lit candle and a door."
" What do you do?" << std::endl;
ActionPointer nextAction = getAction();
while ( nextAction == nullptr ) {
std::cout << "I don't understand. What do you do?" << std::endl;
nextAction = getAction();
}
(*nextAction)();
如果您想在错误消息中获得更多信息,可以安排
使用指针和该信息返回struct
(仍在测试
指针)。
答案 1 :(得分:0)
您可以在外部添加while(true)
循环,并添加if条件,该条件会在正确输入时中断。
此外,在getline命令之后,在else if conditon:
之后添加else条件else
cout << "I don't understand " << startout << ". What do you do?" << endl;
在另一个if
和else if
子句中,添加break;
命令。
答案 2 :(得分:0)
你想跑,直到他们输入正确的东西?这非常简单。你应该使用一个循环。
循环通常是一个语句块,它会在条件满足之前重复。例如,一个好的循环是:
Repeat the program until the string equals Door or Candle
。
条件,我看你已经比较熟悉了。然后让我向您介绍while
循环块的语法,我认为它最适合您的情况(do while
是另一个适合您的循环,但它是&#39;稍微复杂一点来解释)。
编辑:我正在使用rain的评论更改代码:
string startout=""; // Initiallizing it before the while loop, so it could be used inside it.
cout << "You wake up in a room. There is a small lit candle and a door. What do you do?" << endl;
while(startout !="door" && startout !="candle") // Do the following actions in this block until startout equals "Door" or "Candle".
{ // start of a while block
getline(cin, startout);
if (startout.tolower().find("door")!=npos) // If the string contains any variation of door:
{
cout << " " << endl;
cout << "You move to the door but its too dark to see anything besides the outline of it." << endl;
}
else if (startout.tolower().find("candle")!=npos) // same, with candle. Much more elegant, like thomas said.
{
cout << "You pick up the candle then move to the door. With the light from the candle you can see the door well. What do you do?" << endl;
}
else
{
cout << "I don't understand " << startout << ". What do you do?" << endl; // If you didn't get what you want, ask for another string and restart.
}
} // end of the while block.
答案 3 :(得分:-2)
for (;;)
cout << "You wake up in a room. There is a small lit candle and a door. What do you do?" << endl;
string startout;
getline(cin, startout);
if (startout == "Door" || startout == "DOOR" ||
startout == "door" || startout == "Open Door" ||
startout == "open door" || startout == "OPEN DOOR" ||
startout == "Open door" || startout == "Open the door" ||
startout == "open the door") {
cout << " " << endl;
cout << "You move to the door but its too dark to see anything besides the outline of it." << endl;
break;
} else if (startout == "Candle" || startout == "CANDLE" ||
startout == "candle" || startout == "Pick Up Candle" ||
startout == "PICK UP CANDLE" ||
startout == "pick up candle" ||
startout == "Pick up candle") {
cout << "You pick up the candle then move to the door. With the light from the candle you can see the door well. What do you do?" << endl;
break;
};
}
这不是最优雅的方式,但你明白了。