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

  • <legend id='Hojmr'><style id='Hojmr'><dir id='Hojmr'><q id='Hojmr'></q></dir></style></legend>

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

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

        std::cin 不会在错误输入时引发异常

        时间:2023-08-28

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

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

            <tbody id='zecB6'></tbody>

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

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

                1. 本文介绍了std::cin 不会在错误输入时引发异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我只是想写一个简单的程序,从cin读取,然后验证输入是一个整数.如果是这样,我将跳出我的 while 循环.如果没有,我会再次要求用户输入.

                  I am just trying to write a simple program that reads from cin, then validates that the input is an integer. If it does, I will break out of my while loop. If not, I will ask the user for input again.

                  我的程序编译并运行得很好,这很棒.但是如果我输入一个非数值,它不会提示输入新的内容.什么给?

                  My program compiles and runs just fine, which is great. But it doesn't prompt for new input if I enter a non numeric value. What gives?

                  #include <iostream>
                  using namespace std;
                  
                  int main() {
                      bool flag = true;
                      int input;
                      while(flag){
                          try{ 
                              cout << "Please enter an integral value 
                  ";
                              cin >> input;
                              if (!( input % 1 ) || input == 0){ break; }
                          }
                          catch (exception& e)
                          { cout << "Please enter an integral value"; 
                          flag = true;}
                      }
                      cout << input;
                      return 0;
                  }
                  

                  推荐答案

                  C++ iostreams 不使用异常,除非你告诉他们使用 cin.exceptions(/* 异常条件 */).

                  C++ iostreams don't use exceptions unless you tell them to, with cin.exceptions( /* conditions for exception */ ).

                  但是您的代码流更自然,无一例外.只需执行 if (!(cin >> input))

                  But your code flow is more natural without the exception. Just do if (!(cin >> input)), etc.

                  还要记得在重试之前清除失败位.

                  Also remember to clear the failure bit before trying again.

                  整个事情可以是:

                  int main()
                  {
                      int input;
                      do {
                         cout << "Please enter an integral value 
                  ";
                         cin.clear();
                         cin.ignore(std::numeric_limits<std::streamsize>::max(), '
                  ');
                      } while(!(cin >> input));
                      cout << input;
                      return 0;
                  }
                  

                  这篇关于std::cin 不会在错误输入时引发异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何处理或避免 C++ 中的堆栈溢出 下一篇:C++ 错误:将‘char*’赋值给‘char [2] 时的类型不兼容

                  相关文章

                    <tfoot id='m3RRW'></tfoot>

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

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