C + + Gets the difference between character Cin,getchar,get,getline

Source: Internet
Author: User

Original works, reproduced please specify the source: http://www.cnblogs.com/shrimp-can/p/5241544.html

1.cin>>

1) The most common is to get the input of a character or number, such as

int A, B;

cin>>a>>b;

Note that:cin>> automatically filters out invisible characters (such as Space return tab, etc.). If you do not want to filter out whitespace characters, you can use NOSKIPWS flow control.

The following program, does not filter out the invisible characters, the input space characters are stored in the input[1], can also output.

2) gets the input string, which can be used in an array or string type. Such as

Char a[20];

cin>>a;

cout<<a<<endl;

or String type:

string S;

cin>>s;

cout<<s<<endl;

Note: Encountering spaces, carriage returns, and so on will end up getting the input string, and the subsequent string will be filtered out (stored in the input stream). If you need to enter a string later, you will get it starting with the string that was stored earlier.

The end of the space, carriage return, etc.

The PO is stored in a string.

2.cin.get ()

1)cin.get (character variable name), used to receive characters, only one character can receive a space, the end of the carriage return

2)cin.get (array name, number of characters received), used to receive the string, you can receive a space, the end of the carriage return.

Note: The last character of the array will be ' \ s ', set the number of received characters to n, if the input string is greater than or equal to N, then the actual received input is the first n-1 character of the string, including the space (excluding carriage return, the end of a carriage return), will automatically add a '.

3)cin.get (), without parameters, is used primarily to discard unwanted characters from the input stream, or to discard a carriage return, which discards one character from the input stream.

does not use Cin.get (), the following s will continue to read from the input stream

With Cin.get (), the S after H is omitted.

3.cin.getline ()

The actual is Cin.getline (receive string to M, receive number N, end character). Receive a string, you can receive a space, and the last character is ' \ s '. The Terminator can be set by itself by setting the third parameter, which defaults to a carriage return. M cannot be of type string.

Note: The actual received is less than N, because the last character is ' \ s '.

4.getline ()

Used for the string class. Use the header file #include<string> that you want to include. Getline (cin,string s), receives a string that can receive spaces, carriage returns, etc.

Difference from Cin.getline (): 1.cin.getline () is an array that receives the input string, and Getline () is a string type.

2.cin.getline () can receive spaces, but cannot receive carriage returns; Getline () can receive spaces and carriage returns

3.cin.getline () will be ' + ' at the end of the array, getline () will not

5.gets ()

The gets (m) is used for the string class and contains #include<string>. Can receive the space, the return end. Can be used for multidimensional arrays.

6.getchar ()

M=getchar (), must include #include<string>

Reference:

http://blog.csdn.net/jiangxinnju/article/details/20492453

C + + Gets the difference between character Cin,getchar,get,getline

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.