For continuous CIN input data (CIN does not discard carriage return,>> will skip carriage return)

Source: Internet
Author: User
see a lot of C + + code written in Cin.get () after the comment said this is the absorption carriage return, do not know what it means!
See a lot of C + + code written in Cin.get () after the comment said this is the absorption carriage return, do not know what it means!
For example
cout<< "Please enter learn, age and entrance results:" <<endl;
cin>>st1.id>>st1.age>>st1.score;
Cin.get ();
Finally this cin.get () book said its function is to absorb carriage return, please expert guidance!
Characters have always
been difficult to manipulate because you enter the data is a carriage return as the end, but the carriage return is not entered into the program, if the following appear to enter the characters such as statements, then this carriage return is first entered, that is, there will be errors, so in the acquisition of data, Especially when several kinds of data are collected together, pay attention to the problem of carriage return.

The carriage return is left in the buffer, except that the CIN stream has a default state of skipping whitespace for the input operator (i.e. the ">>" operator), so whitespace characters such as carriage return are skipped by default when read with the input operator, which is actually in the buffer, You can use the NOSKIPWS operator to let the input operators not skip whitespace characters, so that you can read "\ n" and other white space, as follows:

//---------------------------------------------------- -----------------------
#include <iostream>

int main (void)
{
        char m,n;
        std::cin>>m;
        std::cin>>std::noskipws>>n;//does not ignore whitespace characters
       	return 0;
}
---------------------------------------------------------------------------the

STD::SKIPWS operator to set the Ignore whitespace character.

Because Cin.get () is getting a character, the carriage return is also a character,
Cin.get () will read the carriage return (' \ n ') as a character. While cin>> skips a carriage return or a space

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.