C + + Primer Plus 6th reading notes-6th branch statements and logical operators

Source: Internet
Author: User
Tags logical operators

1. The handling of line breaks when Cin reads errors
1#include <iostream>2 3 using namespacestd;4 5 intMain () {6     DoubleD;7     CharC;8CIN >>D;9     if(!cin) {Tencout <<"x"<<Endl; One cin.clear (); Acout << cin.Get() <<Endl; -     } -     //cin.get (); theCin.Get(c); -}

The above code input is divided into 2 cases:

If the input is a double type, cin.get (c) reads the newline character and the program finishes running.

If the input is not a double type, such as the char type, then CIN will place the wrong bit, leaving the wrong input with the return character (this is why, guess), so the program stops waiting for input when it runs to Cin.get (c).

Strange, and tested several times, feeling the same as before, the program runs the end.

C + + Primer Plus 6th reading notes-6th branch statements and logical operators

Related Article

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.