Several common methods of inputting strings in C ++ are as follows:
cin、cin.get()、cin.getline()、getline()、gets()、getchar()
1. cin >>
Usage 1: The most common and basic usage, enter a number:
#include <the iostream> the using namespace STD; int main () { int A, B; CIN >> A >> B; COUT << A + B << endl; } Input: 2 [ENTER] 3 [ENTER ] Output: 5
Usage 2: Accept a character string, and it ends when it encounters "space", "Tab", and "carriage return"
#include <the iostream> the using namespace STD; int main () { char A [ 20 is ]; CIN >> A; COUT << A << endl; } Input: jkljkljkl Output: jkljkljkl Input: jkljkl jkljkl // case space End, so you can not enter multiple words Output: jkljkl
2. cin.get()
Usage one: cin.get (character variable name) can be used to receive characters
#include <the iostream> the using namespace STD; int main () { char CH; CH . = CIN GET (); // or cin.get (ch); only get a character COUT CH << << endl; } Input: jljkljkl Output: j
Usage two: cin.get (character array name, the number of characters received) is used to receive a line of strings, can receive spaces
#include <iostream> using namespace std; int main () { char a [ 20 ]; cin. get (a, 20 ); // Some similar to getline. You can enter multiple words, separated by spaces. << << A COUT endl; } Input: jkl jkl jkl output: jkl jkl jkl Input: abcdeabcdeabcdeabcdeabcde (input 25 characters) Output: abcdeabcdeabcdeabcd (19 characters receiving +1 ' \ 0 ' )
Usage three: cin.get (without parameters) without parameters is mainly used to discard the unwanted characters in the input stream, or discard the carriage return to make up for the shortcomings of cin.get (character array name, number of received characters).
#include <iostream> using namespace std; int main () { char arr [ 10 ]; cin. get (arr, 10 ); cin. get (); // used to eat carriage return, equivalent to getchar (); cout << arr << endl; cin. get (arr, 5 ); cout << arr << endl; system ( " pause " ); return 0 ; } input abcdefghi output abcdefghi input abcde output abcd #include<iostream> using namespace std; int main () { char arr [ 10 ]; cin. get (arr, 10 ); // cin.get (); // used to eat carriage return, equivalent to getchar (); Now comment out this line and try cout << arr << endl; cin. Get (arr, 5 ); cout << arr << endl; system ( " pause " ); return 0 ; } input abcdefghi output abcdefghi
3.cin.getline ()
cin.getline () // accept a string, can receive spaces and output
#include <iostream> using namespace std; int main () { char m [ 20 ]; cin.getline (m, 5 ); // basically the same as above. << << m COUT endl; } Input: jkljkljkl Output: jklj receiving characters to 5 m, where the last one is ' \ 0 ' , so only see four character output; if 5 into 20: input: jkljkljkl output: jkljkljkl input: jklf fjlsjf fjsdklf output: jklf fjlsjf fjsdklf // extension: // cin.getline () actually has three parameters, cin.getline (accepts a string to m, the number of receiving 5, end Character) //When the third parameter is omitted, the system defaults to '\ 0' which is '/ n'. // If you change cin.getline () in the example to cin.getline (m, 5, 'a'); when input jlkjkljkl, output jklj, when input jkaljkljkl, output jk when used in a multidimensional array, you can also with cin.getline (m [I], 20 is ) such usage: #include <the iostream> #include < String > the using namespace STD; int main () { char m [ . 3 ] [ 20 is ]; for ( int i = 0 ; i < 3 ; i ++ ) { cout << " \ nPlease enter the first " << i + 1 <<" String: " << endl; cin.getline (m [I], 20 is ); } COUT << endl; for ( int J = 0 ; J < . 3 ; J ++ ) COUT << " Output m [ " J << << " ] value: " << m [J] << endl; } Please enter a string: kskr1 Please enter the second string: kskr2 Please enter characters : string kskr3 output m [ 0 value]: kskr1 output m [ . 1] Value: kskr2 output m [ 2 ] value: kskr3
4. getline()
getline () // accept a string, can receive spaces and output, need to include "#include <string>
#include <the iostream> #include < String > the using namespace STD; main () { String STR; getline (CIN, STR); COUT << STR << endl; } Input: jkljkljkl // VC6 has a bug, need Enter twice. Output: jkljkljkl Input: jkl jfksldfj jklsjfl output: jkl jfksldfj jklsjfl and cin.getline () is similar, but cin.getline () istream stream belongs, and getline () belongs string flow, two functions are not the same
5. gets()
gets () // accept a string, can receive spaces and output, need to include "#include <string>
#include <the iostream> #include < String > the using namespace STD; main () { char m [ 20 is ]; the gets (m); // can not be written as the gets = m (); COUT m << << endl; } Input : jkljkljkl output: jkljkljkl input: jkl jkl jkl output: jkl jkl jkl similar cin.getline () inside an example, gets () can also be used in multi-dimensional arrays which: #include <the iostream> #include < String > the using namespace STD; int main () { char m[3][20]; for(int i=0;i<3;i++) { cout<<"\n请输入第"<<i+1<<"个字符串:"<<endl; gets(m[i]); } cout<<endl; for(int j=0;j<3;j++) cout<<"输出m["<<j<<"]的值:"<<m[j]<<endl; } Please enter a string: kskr1 Please enter the second string: kskr2 Please enter string: kskr3 output m [ 0 ] values: kskr1 output m [ . 1 ] value: kskr2 output m [ 2 value]: kskr3 feel gets () and cin.getline () usage is very similar, but cin.getline () fills one more parameter; Incidentally here, for this article The example of kskr1, kskr2, kskr3 is also applicable to cin >>, because there is no space entered here, if a space is entered, such as "ks kr jkl [carriage return]" then cin will have received 3 strings , "Ks, kr, jkl";
another example is "kskr 1 [carriage return] kskr 2 [carriage return]", then receive "kskr, 1 , kskr"; this is not the result we want! And cin.getline () and gets () will not produce this error because they can receive spaces;
6.getchar()
getchar () // Accept a character, need to include "#include <string>
#include <the iostream> the using namespace STD; int main () { char CH; CH = getchar (); // can not be written getchar (CH); COUT CH << << endl; } Input: jkljkljkl Output: J / / getchar () is a function of C language, C ++ is also compatible, but try not to use it or use it less;