为什么std::cin同时接受多个输入
Why does std::cin accept multiple inputs at the same time?
我为一个基于文本的游戏编写了一段代码,该游戏在每回合开始时接受用户输入,并根据输入做不同的事情。例如,"向上"应将玩家在网格上向上移动1个空间,"看"应显示该空间的指定场景。任何未指定为有效输入的内容都会导致系统输出一条错误消息,"我不明白。"问题是:如果我键入"向上看,向下看,向右看"之类的内容,它会按顺序执行每个命令,而不是显示错误消息。
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;
}
您使用的是"格式化"输入运算符>>
。基本上,格式化的输入操作在看到空白字符时会停止并返回。因此,当您的输入为"向上看,向下看,向右看"时,while循环实际上会为输入中的每个命令执行六次。
如果您不想在一行中使用多个命令,请使用std::getline
:
while(getline(cin, input)) {
if (input == "quit") break;
if (input == "up") ...
}
相关文章:
- 在while循环中输入带有std::cin的字符串后,控制台会输出大量胡言乱语
- 在一个模板函数中,若输入的类型是enum类,我该如何使用std::underlying_type
- 是否有类似std::lower_bound的函数,而不需要排序/分区输入
- 输入std::数组时出现问题
- ";结果类型必须是可从输入范围的值类型""构造的;创建std::vector时
- std::getline没有在while循环中重新请求用户输入
- 在输入句子时终止 std::out_of_range
- 检查输入 std::array 指针数据是否等于某个常量数组
- std::unordered_map 如果输入大小已知,如何优化批量插入
- C++ 限制或使用户输入不可见 (std::cin)
- 为什么 std::cin 不提示用户输入?
- 为什么从 std::cin 读取输入后右括号字符的值有时不是")"?
- 为什么不能通过在错误输入后设置 std::cin.clear() 来使用 std::cin?
- 我想知道为什么"std::unique_ptr<int> foo(新 int)"是合法的,因为"std::<int>unique_ptr"要求输入参数类型应该是"int"?
- 为什么这个程序接受整数和字母数字输入并打印它们? C++ 中的 std::string 也采用整数值吗?
- 当使用 std::CIN 输入结构时,它会出错
- C++ 入门 5th 1.4.4 为什么不能立即输入 std::cout 内容
- 如何将二进制十六进制文字输入 std::istream
- 为什么在gcc-4.9.1-4ubuntu2中没有手动输入std::vector
- 输入Std::cin与直接赋值Std::string变量不一样