C ++ Primer Plus 6th Reading Notes, primer6th

Source: Internet
Author: User

C ++ Primer Plus 6th Reading Notes, primer6th
1. Handling line breaks when cin reads errors

1 # include <iostream> 2 3 using namespace std; 4 5 int main () {6 double d; 7 char c; 8 cin> d; 9 if (! Cin) {10 cout <"x" <endl; 11 cin. clear (); 12 cout <cin. get () <endl; 13} 14 // cin. get (); 15 cin. get (c); 16}

The preceding code is entered in two cases:

If the input is double type, cin. get (c) will read the line break and the program is running.

If the input is not double type, such as char type, the cin will set the error bit, retain the incorrect input while discarding the carriage return (this is the case, the cause is guessed), so the program is running to the cin. get (c) stops waiting for input.

It was strange that the program was tested several times again, and it felt like before that, the program was finished.

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.