C + + line-oriented input getline () and get ()

Source: Internet
Author: User

Source: C + + Primer Plus

In C + + it is customary to use CIN when we enter a string, but Cin can only read a string with no spaces, and if we need to read a string containing a space, we need to use getline () or get (). The following describes Getline () and get (), both of which are read to the end of the line break.

1.getline ():

Let's say we're going to enter a name and save it in the name array:

#include <iostream>
using namespace Std;
int main ()
{
Char name[10];
Cin.getline (name,10);
cout<<name<<endl;
return 0;
}
First time input: 123456 First output: 123456
Second input: 1234567890 second output: 123456789

Call mode is Cin.getline (), there are two parameters, respectively, is the array name and the length of the group, it is important to note that if the length is 10, then a maximum of 9 characters will be read, because there is a position to leave the null character ' ", if only 5, then the rest will fill with empty characters. For Getline (), it reads until it encounters a newline character or reaches the specified number of characters, and then the newline character is discarded, not left in the input team joins (which is the subtle difference between the next and get ()).

Getline () can also be used like this:

#include <iostream>using namespacestd;intMain () {Charname1[Ten],name2[Ten]; Cin.getline (name1,Ten). Getline (Name2,Ten);
The first call returns a Cin object, and the object then calls Getline () cout<<name1<<' '<<name2<<Endl; return 0;}/*input: 123 456 output: 123 456*/

2.get ():

Get () and getline () are similar, but also two parameters, but get () read to the newline character stop, the line break will remain in the input queue, the next time read will be read again to the newline character, causing the read to be empty.

For example:

#include <iostream>using namespacestd;intMain () {Charname1[Ten],name2[Ten]; Cin.Get(Name1,Ten); Cin.Get(Name2,Ten); cout<<name1<<' '<<name2<<Endl; return 0;}/*input: 123 output: 123*/

Here I would like to enter 123 and then enter 456, but I do not have a chance, because the input 123 encountered a newline character and then stop, the second call when the line break is still in the input queue, the program will stop again, after the content is not read.

When Cin.get () does not have a parameter, it draws a character, including newline characters, so we can:

#include <iostream>using namespacestd;intMain () {Charname1[Ten],name2[Ten]; Cin.Get(Name1,Ten); Cin.Get(); Cin.Get(Name2,Ten); cout<<name1<<' '<<name2<<Endl; return 0;}/*input: 123456 output: 123 456*/

You can also write this:

#include <iostream>using namespacestd;intMain () {Charname1[Ten],name2[Ten]; Cin.Get(Name1,Ten).Get(); Cin.Get(Name2,Ten); cout<<name1<<' '<<name2<<Endl; return 0;}/*input: 123456 output: 123 456*/

Empty rows and out of allocated space can cause errors and should be avoided as much as possible.

What's more, when we're actually using a mix of numbers and strings, it's easy to make mistakes, and here's an example from the book:

#include <iostream>using namespacestd;intMain () {intYear ; Charaddress[ -]; CIN>>Year ; Cin.getline (Address, -); cout<<year<<Endl; cout<<address<<Endl; return 0;}/*input/output: 20182018*/

We entered the year, this time the line break is still in the input queue, and then call Getline () encountered newline character, directly end the read ... , where you can use Cin.get () to absorb line breaks.

That's pretty much it.

C + + line-oriented input getline () and get ()

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.