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