Cin.get (), Cin.getline (), getline () difference

Source: Internet
Author: User

There are two getline functions in C + +, each defined in a different header file

1, getline () is defined in the <string> header file, the function is to take a line of string, read to the end of the newline character R, and discard the newline character, if you need to read the characters, then the next line read.

Getline (CIN,STR); The first argument is the input stream object, and the second argument is a string object that takes the IStream parameter as the return value. (Getline (CIN, line))

2, Cin.getline () is a member function of CIN, defined in the <iostream> header file, the function has two parameters, the first parameter is used to store the input row of the name of the array, the second parameter is to read the number of characters, if this parameter is 20, then the letter The number reads up to 19 characters, the remaining space is used to store the empty characters that are automatically added at the end, and the getline () member function stops reading the specified number of characters or encounters a newline character, as in the above, such as Cin.getline (name, 20);

3, Cin.get () There are two ways, the first is similar to Cin.getline (), they accept the same parameters, the same way to interpret the parameters, and all read to the end of the line, but get is not read and discard the newline character, but leave it in the input queue. If you finish a cin.get (name, size) and then use Cin.get (name, size), the new line is not read, only the newline character is read, and the second method is Cin.get () with no arguments, which reads a character, including newline characters.

Cin.getline (name, 20); with Cin.get (name,); Cin.get (); same effect

Cin.get (), Cin.getline (), getline () difference

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.