C + + input line string getline/get/getline

Source: Internet
Author: User

Getline (): For character arrays
Reads the entire row of data, using the line break entered by the ENTER key to determine the end of the input. getline () discards the line break.
Calling method: Cin.getline (str, len);
The first parameter, STR, is the array name used to store the input rows, and the second parameter, Len, is the number of characters to read.

1#include <iostream>2 using namespacestd;3  4 intMain ()5 {6     Charstr[ -];7Cin.getline (str, -);8cout << str <<Endl;9     return 0;Ten}
View Code

Get (): For character arrays

Reads the entire row of data, using the line break entered by the ENTER key to determine the end of the input. get () keeps line breaks in the input sequence.

When you use Cin.get () to enter multiple rows of data, you can use get () to eliminate line breaks in the middle. Because get (str, len) and get () are all class members of CIN, they can be combined to write.

Calling method: Cin.get (str, len);

The first parameter, STR, is the array name used to store the input rows, and the second parameter, Len, is the number of characters to read.

1#include <iostream>2 using namespacestd;3  4 intMain ()5 {6     Charstr1[ -], str2[ -];7Cin.Get(STR1, -);8Cin.Get();9 Ten     /*cin.get (str1,). get (); Watch this .*/ One  ACin.Get(STR2, -); -cout <<"str1:"<< str1 <<Endl; -cout <<"str2:"<< str2 <<Endl; the     return 0; -}
View Code

Getline (): for the String class

1#include <iostream>2#include <string>3 using namespacestd;4  5 intMain ()6 {7     stringstr;8 getline (CIN, str);9cout << str <<Endl;Ten     return 0; One}
View Code

C + + input line string getline/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.