• <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?

        Why does while (true) skip cin when it received invalid input?(为什么 while (true) 在收到无效输入时会跳过 cin?)
        <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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  What is inside .lib file of Static library, Statically linked dynamic library and dynamically linked dynamic library?(静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么?)
                  How do I load a C DLL from the SXS in Python?(如何从 Python 中的 SXS 加载 C DLL?)
                  Can Cython code be compiled to a dll so C++ application can call it?(Cython 代码可以编译成 dll 以便 C++ 应用程序可以调用它吗?)
                  Delay Loading DLLs(延迟加载 DLL)
                  Throwing C++ exceptions across DLL boundaries(跨 DLL 边界抛出 C++ 异常)
                  Loading a dll from a dll?(从 dll 加载 dll?)
                  <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>
                      <tbody id='WPdD2'></tbody>

                    • <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>