An explanation of the value process of CIN object

Source: Internet
Author: User

  • suddenly empty, in view of the Cin object to the value of the mechanism is not very understanding, to explore, and write down, easy to review later.
    1#include <iostream>2  3  intMainvoid)4  {5      using namespacestd;6      intDate =2;7      Charch;8       while(! (Cin >>date)) {9cout <<"Not a number.\n";Tencout << Date <<Endl; OneCin.clear ();//without this, this function will always output "not a number", A          //The reason is that after a read failure, the CIN object sets the Fail bit (failbit), causing the subsequent input to be blocked -CIN >>ch; -cout << Ch <<Endl; the      }    -cout << Date <<Endl; -      return 0; -  } +  /************************** - * Q + * Not a number. A * 0 at * Q - * 4 - * 4 -   * ************************/ -~

      explanation:
      Line 8th: date is int, but I deliberately input a character q, so cin>>ch returns false, True in the while parenthesis to execute the while loop.

              Line 10th:   from the code, you know that date has been initialized to 2, but in the output, The date we see has become 0, indicating that the CIN object will take the value "2" in the date variable to kick away,   and then put the value entered in Cin>>date into date, but in line eighth I deliberately input a q, Unlike date itself, it is not possible to put Q into date. The last date can only be 0. (For variables with no value, the default is 0)

      Line 11th: After failing to set a value for date, Q remains in the input stream and "lock the input stream" (the error flag is set), which means that subsequent statements cannot be evaluated from the input stream. Use Clear () is "unlock".

              Line 13th: After unlocking, you can take the Q that is left in the input stream, so the CH value of output in line 14 is Q.

      While: After executing 14 lines, the judgment inside the while parenthesis is returned. At this point the Cin object asks you to enter from the keyboard, this time enter 4, so the while loop does not execute, after the 16th line outputs the value of date 4, the program ends.

  • This paragraph is a book about type mismatch description:
    Initial conditions (int n; CIN >> N)
    What happens if I enter a word instead of a number?
    The 1,n value remains the same;------> here seems to be rushing with my conclusions above.
    2, the mismatched input will be left in the input queue;
    An error flag for the 3,cin object is set, —————— means that no more digits can be entered and reset with clear ().
    4, the call to the Cin method will return False (bool type). ———— refers to the while or if.
    Look at the 1th, it's obvious to me that " kick the value in the date variable" 2 "out of the way" contradiction:
    1#include <iostream>2  3  intMainvoid)4  {5      using namespacestd;6      intn =5;7CIN >>N;8  9 cin.clear ();Ten      Charch; OneCIN >>ch; Acout <<"Ch:"<< CH <<Endl; -   -cout << "N:" << n <<Endl; the      return 0; -   -  } -  /*************************** + * e - * Ch:e + * n:0 A   * ************************/

    Conclusion: As seen in the program output above, the value of n will be valued at 0 when the input type does not match. Big question, is it my understanding that the phrase "n values remain constant" is wrong? This sentence is to be set up under certain premises???? First left, later understand to fill the hole. Don't wait, now know why: This n value remains constant refers to the mismatch of the type is not put into N, that's all. But the system can reset the n value, so we see that the N initialized to 5 becomes 0.
    Look at the other things:

    1#include <iostream>2  3  Const intMax =5;4  5  intMainvoid)6  {7      using namespacestd;8      intTest[max];9  Ten      inti; One       for(i =0; i < Max; ++i) { Acout <<"number#"<< I <<": "; -           while(! (Cin >>Test[i])) { -Cin.clear ();//Reset Input the               while(CIN.)Get() !='\ n') -                  Continue; -cout <<"Please enter a number:"; -          } +      }    -   +cout <<"You enter:"; A       for(inti =0; i < Max; ++i) { atcout << Test[i] <<"  "; -      }    -cout <<Endl; -   -      return 0; -  } in   -  /************************************** to * Number#0:11 + * Number#1:12 - * Number#2:13 the * NUMBER#4:QWEFGF * * Please enter a number:16 $ * you enter:11Panax Notoginseng   * ************************************/

    Other things in the code, take a good look.


An explanation of the value process of CIN object

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.