• <bdo id='gpiKh'></bdo><ul id='gpiKh'></ul>

    <legend id='gpiKh'><style id='gpiKh'><dir id='gpiKh'><q id='gpiKh'></q></dir></style></legend>
  • <small id='gpiKh'></small><noframes id='gpiKh'>

        <tfoot id='gpiKh'></tfoot>

        <i id='gpiKh'><tr id='gpiKh'><dt id='gpiKh'><q id='gpiKh'><span id='gpiKh'><b id='gpiKh'><form id='gpiKh'><ins id='gpiKh'></ins><ul id='gpiKh'></ul><sub id='gpiKh'></sub></form><legend id='gpiKh'></legend><bdo id='gpiKh'><pre id='gpiKh'><center id='gpiKh'></center></pre></bdo></b><th id='gpiKh'></th></span></q></dt></tr></i><div id='gpiKh'><tfoot id='gpiKh'></tfoot><dl id='gpiKh'><fieldset id='gpiKh'></fieldset></dl></div>

        为什么 while (true) 在收到无效输入时会跳过 cin?

        时间:2023-08-28
        <tfoot id='LX4Ko'></tfoot>
        <i id='LX4Ko'><tr id='LX4Ko'><dt id='LX4Ko'><q id='LX4Ko'><span id='LX4Ko'><b id='LX4Ko'><form id='LX4Ko'><ins id='LX4Ko'></ins><ul id='LX4Ko'></ul><sub id='LX4Ko'></sub></form><legend id='LX4Ko'></legend><bdo id='LX4Ko'><pre id='LX4Ko'><center id='LX4Ko'></center></pre></bdo></b><th id='LX4Ko'></th></span></q></dt></tr></i><div id='LX4Ko'><tfoot id='LX4Ko'></tfoot><dl id='LX4Ko'><fieldset id='LX4Ko'></fieldset></dl></div>

              <bdo id='LX4Ko'></bdo><ul id='LX4Ko'></ul>

              <small id='LX4Ko'></small><noframes id='LX4Ko'>

              • <legend id='LX4Ko'><style id='LX4Ko'><dir id='LX4Ko'><q id='LX4Ko'></q></dir></style></legend>
                  <tbody id='LX4Ko'></tbody>

                1. 本文介绍了为什么 while (true) 在收到无效输入时会跳过 cin?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  这个while循环在接收到错误的输入(非整数)后不等待来自cin的输入.cin 是否以某种方式保持在虚假状态?

                  This while-loop does not wait for input from cin after receiving wrong input (non-integer). Does cin somehow stay in a false state?

                  while (true) {
                      int x {0};
                      cout << "> ";
                      cin >> x;
                      cout << "= " << x << endl;
                  }
                  

                  我希望这个 while 循环每次都等待输入,但是当它接收到错误的输入时不再发生这种情况.

                  I would expect this while-loop to wait for input everytime around, but that no longer happens when it receives wrong input.

                  推荐答案

                  一旦 cin 失败,它会一直处于无效状态,直到被清除.

                  Once cin fails, it stays in an invalid state until it's cleared.

                  clear() 不带参数可用于取消设置意外输入后的故障位

                  clear() without arguments can be used to unset the failbit after unexpected input

                  通过为它们分配值来设置流错误状态标志状态.默认情况下,分配具有效果的 std::ios_base::goodbit清除所有错误状态标志.

                  Sets the stream error state flags by assigning them the value of state. By default, assigns std::ios_base::goodbit which has the effect of clearing all error state flags.

                  正如 @Peter 指出的那样,您还必须清除缓冲区.

                  As @Peter points out, you also have to clear the buffer.

                  你的例子是这样的:

                  while (true) {
                    int x{0};
                    cout << "> ";
                    if (!cin) {
                      // unset failbit
                      cin.clear();
                      // clear the buffer
                      cin.ignore(numeric_limits<streamsize>::max(), '
                  ');
                    }
                    cin >> x;
                    cout << "= " << x << endl;
                  }
                  

                  这篇关于为什么 while (true) 在收到无效输入时会跳过 cin?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Cin in a while 循环 下一篇:我要求用户输入大小和数组,但是当我打印矢量时,它仅显示“0"作为输出

                  相关文章

                  <legend id='WPdD2'><style id='WPdD2'><dir id='WPdD2'><q id='WPdD2'></q></dir></style></legend>

                  <small id='WPdD2'></small><noframes id='WPdD2'>

                  • <bdo id='WPdD2'></bdo><ul id='WPdD2'></ul>

                2. <tfoot id='WPdD2'></tfoot>

                    <i id='WPdD2'><tr id='WPdD2'><dt id='WPdD2'><q id='WPdD2'><span id='WPdD2'><b id='WPdD2'><form id='WPdD2'><ins id='WPdD2'></ins><ul id='WPdD2'></ul><sub id='WPdD2'></sub></form><legend id='WPdD2'></legend><bdo id='WPdD2'><pre id='WPdD2'><center id='WPdD2'></center></pre></bdo></b><th id='WPdD2'></th></span></q></dt></tr></i><div id='WPdD2'><tfoot id='WPdD2'></tfoot><dl id='WPdD2'><fieldset id='WPdD2'></fieldset></dl></div>