C + + learning journey get, Getline usage

Source: Internet
Author: User

C + + learning journey get, Getline usage

Line-oriented input: Cin.getline ().
The function reads the entire line, which uses the newline character entered by the ENTER key to determine the end of the input. To call this method, you can use Cin.getline (). The function has two parameters. The first parameter is the array name used to store the input rows, and the second argument is the number of characters to read. If this parameter is 20, the function reads up to 19 characters, and the remaining space is used to store the empty characters that are automatically added at the end. The Getline () member function stops reading when a specified number of characters are read or when a newline character is encountered.



Line-oriented input: Cin.get (). The IStream class has another member function called Get (), which has several variants, one of which works like Getline (), receives the same parameters, interprets the same parameters, and reads to the end of the line each time. But get does not read and discard line breaks, but leaves them in the input queue. Suppose we call get () twice in a row;

cin.get(name, ArSize);cin.get(dessert, ArSize);


由于第一次调用后,换行符将留在输入队列中,因此第二次调用时看到第一个字符便是换行符。因此get()认为已经到达行尾,而没有发现任何可读取的内容。如果不借助与帮助,get将不能跳过换行符。幸运的是get()有另外一种变体,cin.get()它可以吃掉换行符。

Like what:

cin.get(name, ArSize);cin.get();cin.get(dessert, ArSize);


Another usage is to stitch together two member functions (merge)

cin.get(name, ArSize).get()

The reason for this is because Cin.get (name, Arsirze) returns a Cin object that is then used to call get ()


note that some older versions of C + + do not implement a get () variant that does not accept any parameters, but implement a get () variant that accepts a char parameter, which requires first declaring a char variable.

char ch;cin.get(name, ArSize).get(ch);

Blank lines and other issues:

What happens when getline () or get () reads empty rows?
Initial practice: The next statement will begin reading at the point where the previous getline () or get () end reads.
However, the current practice is to set the fail bit (falibit) when get () reads to a blank line. This means that the next input will be blocked.


However, you can use the following command to restore the input:

  cin.clear();

Another potential problem is that the input string may be longer than the allocated space. If the input row contains characters transmitting the specified word than characters, then getline () and get () leave the remaining characters in the input queue, and getline () also sets the fail bit and closes the input after it.

One point to note is that C + + allows functions to have multiple versions, provided the parameters list for these versions is different. If you are using Cin.get (name, arsize), the compiler knows that you want to put a string into the array, so the appropriate member function will be used. If you are using Cin.get (), the compiler knows to read a character ——— This is a function overload

Why use Get () instead of getline ()? Because the old-fashioned implementation is not getline (), the second get () makes the input more careful, for example, assuming that a row is read into the array with get (). How do you know if the reason to stop reading is because the entire line is read (newline character) or because the array is already filled? Look at the next input character, and if it is a newline, it means that the entire line has been read, otherwise there are other inputs in the line. In short, getline () is simpler to use, but get () makes it easier to check for errors. You can use either of these to read a line of input. But we need to know that their behavior is somewhat different.

C + + learning journey get, Getline usage

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.