C + + Cin.ignore ()

Source: Internet
Author: User

Say ignore before talking about CIN this I/o input keyword.

CIN requires a "carriage return" to submit the data!!!!! ----------this is very important!

#include <iostream>using namespacestd;voidMain () {intA; Cin.ignore (1,'\ n'); CIN>>A; cout<<a<<Endl; intb; Cin.ignore (2,'\ n'); CIN>>b; cout<<b<<Endl; intC; Cin.ignore (2,'\ n'); CIN>>B; cout<<c<<Endl;}

I entered in turn

123 return Car 123 return car 123 return car

Last output: 23

123

123

The following explains why the following ignore output such a result:

1, enter the first 123 is because the front does not enter a carriage return
2. After you lose the first 123, you will definitely enter a carriage return.
Because CIN this thing asks to submit the data with the carriage return, Cheng continues to execute, this time executes to the Ignore function, it is directly to judge your input buffer, then found the first 123 after the carriage return, therefore the Ignore function is invalid

3, so after you enter any character, with a carriage return will be directly output

C + + Cin.ignore ()

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.