为什么 std::cin 同时接受多个输入?
Why does std::cin accept multiple inputs at the same time?
我为基于文本的游戏编写了一个代码,该游戏在每个回合开始时接受用户输入,并且应该根据输入执行不同的操作。例如,"up" 应将玩家在网格上向上移动 1 space,而 "look" 应显示该 space 的指定场景。任何未指定为有效输入的内容都会导致系统输出错误消息,"I didn't understand that." 问题是:如果我键入类似 "up look down look right right" 的内容,它将按顺序执行每个命令,而不是显示错误留言。
string input = "";
while(input!="quit")
{
cin >> input;
if (input == "left") {
if (map[player1.y][player1.x - 1].isWall==true)
cout << map[player1.y][player1.x - 1].scene;
else if (player1.x > 0)
player1.x -= 1;
else
cout << "You walked off the face of the Earth, but grabbed the ledge just before it was too late. \n Somehow, you managed to gather enough strength to pull yourself back up. \n ...Maybe don't try that again." << endl;
}
else if (input == "right") {
if (map[player1.y][player1.x + 1].isWall==true)
cout << map[player1.y][player1.x + 1].scene << endl;
else if (player1.x < cols-1)
player1.x += 1;
else
cout << "You walked off the face of the Earth, but grabbed the ledge just before it was too late. \n Somehow, you managed to gather enough strength to pull yourself back up. \n ...Maybe don't try that again." << endl;
}
else if (input == "up") {
if (map[player1.y - 1][player1.x].isWall==true)
cout << map[player1.y - 1][player1.x].scene;
else if (player1.y > 0)
player1.y -= 1;
else
cout << "You walked off the face of the Earth, but grabbed the ledge just before it was too late. \n Somehow, you managed to gather enough strength to pull yourself back up. \n ...Maybe don't try that again." << endl;
}
else if (input == "down") {
if (map[player1.y + 1][player1.x].isWall==true)
cout << map[player1.y + 1][player1.x].scene;
else if (player1.y < rows-1)
player1.y += 1;
else
cout << "You walked off the face of the Earth, but grabbed the ledge just before it was too late. \n Somehow, you managed to gather enough strength to pull yourself back up. \n ...Maybe don't try that again." << endl;
}
else if (input == "look")
map[player1.y][player1.x].look();
else if (input == "take")
player1.pickupCons(map[player1.y][player1.x].potions[0]);
else
{
cout << "I don't understand... type something else!" << endl;
continue;
}
您正在使用 'formatted' 输入运算符 >>
。基本上,格式化输入操作会在看到白色 space 字符时停止并 return 。因此,对于您的输入 'up look down look right right',您的 while 循环实际上为输入中的每个命令执行了六次。
如果您不想在一行中执行多个命令,请改用 std::getline
:
while(getline(cin, input)) {
if (input == "quit") break;
if (input == "up") ...
}
我为基于文本的游戏编写了一个代码,该游戏在每个回合开始时接受用户输入,并且应该根据输入执行不同的操作。例如,"up" 应将玩家在网格上向上移动 1 space,而 "look" 应显示该 space 的指定场景。任何未指定为有效输入的内容都会导致系统输出错误消息,"I didn't understand that." 问题是:如果我键入类似 "up look down look right right" 的内容,它将按顺序执行每个命令,而不是显示错误留言。
string input = "";
while(input!="quit")
{
cin >> input;
if (input == "left") {
if (map[player1.y][player1.x - 1].isWall==true)
cout << map[player1.y][player1.x - 1].scene;
else if (player1.x > 0)
player1.x -= 1;
else
cout << "You walked off the face of the Earth, but grabbed the ledge just before it was too late. \n Somehow, you managed to gather enough strength to pull yourself back up. \n ...Maybe don't try that again." << endl;
}
else if (input == "right") {
if (map[player1.y][player1.x + 1].isWall==true)
cout << map[player1.y][player1.x + 1].scene << endl;
else if (player1.x < cols-1)
player1.x += 1;
else
cout << "You walked off the face of the Earth, but grabbed the ledge just before it was too late. \n Somehow, you managed to gather enough strength to pull yourself back up. \n ...Maybe don't try that again." << endl;
}
else if (input == "up") {
if (map[player1.y - 1][player1.x].isWall==true)
cout << map[player1.y - 1][player1.x].scene;
else if (player1.y > 0)
player1.y -= 1;
else
cout << "You walked off the face of the Earth, but grabbed the ledge just before it was too late. \n Somehow, you managed to gather enough strength to pull yourself back up. \n ...Maybe don't try that again." << endl;
}
else if (input == "down") {
if (map[player1.y + 1][player1.x].isWall==true)
cout << map[player1.y + 1][player1.x].scene;
else if (player1.y < rows-1)
player1.y += 1;
else
cout << "You walked off the face of the Earth, but grabbed the ledge just before it was too late. \n Somehow, you managed to gather enough strength to pull yourself back up. \n ...Maybe don't try that again." << endl;
}
else if (input == "look")
map[player1.y][player1.x].look();
else if (input == "take")
player1.pickupCons(map[player1.y][player1.x].potions[0]);
else
{
cout << "I don't understand... type something else!" << endl;
continue;
}
您正在使用 'formatted' 输入运算符 >>
。基本上,格式化输入操作会在看到白色 space 字符时停止并 return 。因此,对于您的输入 'up look down look right right',您的 while 循环实际上为输入中的每个命令执行了六次。
如果您不想在一行中执行多个命令,请改用 std::getline
:
while(getline(cin, input)) {
if (input == "quit") break;
if (input == "up") ...
}