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

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

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

    1. <tfoot id='wkV53'></tfoot>
    2. 重置cin流状态C++

      时间:2023-06-04
        <bdo id='iqUml'></bdo><ul id='iqUml'></ul>

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

            <tbody id='iqUml'></tbody>
          <legend id='iqUml'><style id='iqUml'><dir id='iqUml'><q id='iqUml'></q></dir></style></legend>

            <i id='iqUml'><tr id='iqUml'><dt id='iqUml'><q id='iqUml'><span id='iqUml'><b id='iqUml'><form id='iqUml'><ins id='iqUml'></ins><ul id='iqUml'></ul><sub id='iqUml'></sub></form><legend id='iqUml'></legend><bdo id='iqUml'><pre id='iqUml'><center id='iqUml'></center></pre></bdo></b><th id='iqUml'></th></span></q></dt></tr></i><div id='iqUml'><tfoot id='iqUml'></tfoot><dl id='iqUml'><fieldset id='iqUml'></fieldset></dl></div>
                <tfoot id='iqUml'></tfoot>
              • 本文介绍了重置cin流状态C++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                这里我试图从用户那里获取一个整数,在输入正确时循环.

                Here Im trying to get an integer from user, looping while the input is correct.

                输入非整数值(例如dsdfgsdg")后,cin.fail() 返回 true,正如预期的那样,while 循环体开始执行.

                After entering non integer value (e.g "dsdfgsdg") cin.fail() returns true, as expected and while loop body starts executing.

                这里我使用cin.clear()来重置cin的错误标志;正如预期的那样,cin.fail() 返回 false.

                Here I reset error flags of cin, using cin.clear(); and cin.fail() returns false, as expected.

                但是下一次调用 cin 不起作用并重新设置错误标志.

                But next call to cin doesn't work and sets error flags back on.

                有什么想法吗?

                #include<iostream>
                using namespace std;
                
                int main() {
                  int a;
                  cin >> a;
                
                  while (cin.fail()) {
                    cout << "Incorrect data. Enter new integer:
                ";
                    cin.clear();
                    cin >> a;
                  }
                } 
                

                推荐答案

                cin.clear() 之后,你这样做:

                After cin.clear(), you do this:

                #include <iostream> //std::streamsize, std::cin
                #include <limits> //std::numeric_limits
                ....
                std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '
                ')
                

                上面的作用是清除输入流中仍留在那里的任何字符.否则 cin 将继续尝试读取相同的字符并失败

                What the above does is that it clears the input stream of any characters that are still left there. Otherwise cin will continue trying to read the same characters and failing

                这篇关于重置cin流状态C++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:将输入管道输入到 C++ 程序中以在 Visual Studio 中进行调试 下一篇:读取由空格或换行符分隔的输入...?

                相关文章

                <tfoot id='HHqsY'></tfoot>

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

                      <bdo id='HHqsY'></bdo><ul id='HHqsY'></ul>
                  1. <i id='HHqsY'><tr id='HHqsY'><dt id='HHqsY'><q id='HHqsY'><span id='HHqsY'><b id='HHqsY'><form id='HHqsY'><ins id='HHqsY'></ins><ul id='HHqsY'></ul><sub id='HHqsY'></sub></form><legend id='HHqsY'></legend><bdo id='HHqsY'><pre id='HHqsY'><center id='HHqsY'></center></pre></bdo></b><th id='HHqsY'></th></span></q></dt></tr></i><div id='HHqsY'><tfoot id='HHqsY'></tfoot><dl id='HHqsY'><fieldset id='HHqsY'></fieldset></dl></div>
                  2. <legend id='HHqsY'><style id='HHqsY'><dir id='HHqsY'><q id='HHqsY'></q></dir></style></legend>