zoukankan      html  css  js  c++  java
  • cin 对象取值过程详解

    • 突然又空,鉴于对cin对象的去值机制还不是很了解,就探究一番,并且记下来,方便以后复习。
       1 #include <iostream>
       2  
       3  int main(void)
       4  {
       5      using namespace std;
       6      int date = 2;
       7      char ch; 
       8      while (!(cin >> date)){
       9          cout << "Not a number.
      ";
      10          cout << date << endl;
      11          cin.clear();//若没有这句,这个函数会一直输出“Not a number”,
      12          //原因是读取失败后,cin对象会设置失效位(failbit),导致接下来的输入被阻断
      13          cin >> ch; 
      14          cout << ch << endl;
      15      }   
      16      cout << date << endl;
      17      return 0;
      18  }
      19  /**************************
      20   * q
      21   * Not a number.
      22   * 0
      23   * q
      24   * 4
      25   * 4
      26   * ************************/
      27  ~                                  

       解释:
          第 8 行:date是int型,但我故意输入一个字符q,所以cin>>ch返回false,while括号内为true,执行while循环.

                第10行:   从代码中,可以知道date已经被初始化为2,但输出结果中,我们看到的date却变成0了,说明cin对象在取值时,会先将date变量中的值"2"踢走,   然后再将在cin>>date中输入的值放进date里,但在第八行我故意输入一个q,和date本身的类型不一样,所以无法将q放进date里.最后date只能是0了.(对于没有值的变量,默认是0)

          第11行:在对date设值失败后,q留在了输入流中,并且“锁定了输入流”(错误标志被设置),也就是说后面的语句再也不能从输入流取值了。用clear()是“解锁”。

                第13行:解锁之后,就可以将留在输入流中的q取走了,所以在14行输出的ch值就是q。

          while:执行完14行后,又退回while括号内的判断。此时cin对象会要求你从键盘输入,这次输入4,所以while循环不执行,在第16行输出date的值4后,结束程序。

    • 这一段是书本关于类型不匹配的描述:
      初始条件(int n ;   cin >> n)
      如果输入的是单词而不是数字会怎样呢?
      1,n值保持不变;------>这里好像和我上面的结论相冲.
      2,不匹配的输入将被留在输入队列里;
      3,cin对象的一个错误标志被设置;  ——————意味着不能再输入数字,要用clear()重置。
      4,对cin方法的调用将返回false(bool型)。
      ————指的是在while或if中。
      看第一点,很明显与我上面说的"将date变量中的值"2"踢走"矛盾:
       1 #include <iostream>
       2  
       3  int main(void)
       4  {
       5      using namespace std;
       6      int n = 5;
       7      cin >> n;
       8  
       9      cin.clear();
      10      char ch; 
      11      cin >> ch; 
      12      cout << "ch: " << ch << endl;
      13  
      14      cout << "n: " << n << endl;
      15      return 0;
      16  
      17  }
      18  /***************************
      19   * e
      20   * ch: e
      21   * n: 0
      22   * ************************/

      结论:像上面程序输出所见,n的值在输入类型不匹配时,会被重值为0。大大的疑问,难道是我对这句话“n值保持不变”理解错了?这句话要在某些前提下才成立????先撇下,以后懂了来补洞。不用等了,现在就知道原因了:这个n值保持不变是指不匹配的类型没有被放进n里,仅此而已。但系统可以重置n值,所以我们见到了初始化为5的n变成了0.
      看看其他的情况:

       1  #include <iostream>
       2  
       3  const int Max = 5;
       4  
       5  int main(void)
       6  {
       7      using namespace std;
       8      int test[Max];
       9  
      10      int i;
      11      for (i = 0 ; i < Max ; ++i){
      12          cout << "number#" << i << ": ";
      13          while (!(cin >> test[i])){
      14              cin.clear();  //reset input
      15              while (cin.get() != '
      ')
      16                  continue;
      17              cout << "Please enter a number:";
      18          }
      19      }   
      20  
      21      cout << "You enter: ";
      22      for (int i = 0 ; i < Max ; ++i){
      23          cout << test[i] << "  ";
      24      }   
      25      cout << endl;
      26  
      27      return 0;
      28  }
      29  
      30  /**************************************
      31   * number#0: 11
      32   * number#1: 12
      33   * number#2: 13
      34   * number#4: qwefgf
      35   * Please enter a number:16
      36   * You enter: 11  12  13  15  16  
      37   * ************************************/

      其他的情况在代码里,要好好看看。


  • 相关阅读:
    AcWing
    AcWing
    AcWing
    AcWing
    AcWing
    2019牛客国庆集训派对day1
    模板
    2019南昌网络赛H The Nth Item 矩阵快速幂
    ACwing92 递归实现指数型枚举 dfs
    ACwing91 最短Hamilton路径 状压dp
  • 原文地址:https://www.cnblogs.com/busui/p/5728246.html
Copyright © 2011-2022 走看看