C++ getline of gets of and other functions in detail

  • 2020-07-21 09:27:33
  • OfStack

In the process of learning C++, problems of input and output are often encountered. The following is a summary of the usage of the following functions:

1), cin
2), cin. get ()
3), cin. getline ()
4), getline ()
5), gets ()

1, cin > >

Usage 1: The most basic and common usage, enter 1 number:


 #include <iostream>
  using namespace std;
  main ()
  {
  int a,b;
  cin>>a>>b;
  cout<<a+b<<endl;
  }

Input: 2[Enter]3[Enter]
Output: 5

Usage 2: Accept 1 string, end with "space", "TAB", "enter"


 #include <iostream>
  using namespace std;
  main ()
  {
  char a[20];
  cin>>a;
  cout<<a<<endl;
  }
[

Input: jkljkljkl
Output: jkljkljkl
Input: jkljkl jkljkl // End of blank
Output: jkljkl

]

2, cin. get ()

Usage 1: cin. get(character variable name) can be used to receive characters


 #include <iostream>
  using namespace std;
  main ()
  {
  char ch;
  ch=cin.get(); // or cin.get(ch);
  cout<<ch<<endl;
  }
[

Input: jljkljkl
Output: j

]

Usage 2: cin.get (character array name, number of characters to receive) is used to receive 1 line string, can receive Spaces


 #include <iostream>
  using namespace std;
  main ()
  {
  char a[20];
  cin.get(a,20);
  cout<<a<<endl;
  }
[

Input: jkl jkl jkl
Output: jkl jkl jkl
Input: abcdeabcdeabcdeabcdeabcde (enter 25 characters)
Output: abcdeabcdeabcdeabcd (receive 19 characters +1 '\0')

]

Usage 3: ES81en. get(no arguments) No arguments are mainly used to discard unwanted characters in the input stream, or to discard carriage return to make up for the shortage of ES83en. get(character array name, number of characters received).

3, cin. getline() // accept 1 string, can receive space and output


  #include <iostream>
  using namespace std;
  main ()
  {
  char m[20];
  cin.getline(m,5);
  cout<<m<<endl;
  }

[

Input: jkljkljkl
Output: jklj

]

Accept 5 characters into m, the last of which is '\0', so you only see 4 characters output;

If I change 5 to 20:

[

Input: jkljkljkl
Output: jkljkljkl
Input: jklf fjlsjf fjsdklf
Output: jklf fjlsjf fjsdklf

]

// Extension:  
  //cin.getline() There are actually 3 The parameters, cin.getline( Take a string look at that one m, Accept the number of 5, The end of the character ) 
  // When the first 3 When the parameters are omitted, the system defaults to '\0' 
  // If you take the example cin.getline() Instead of cin.getline(m,5,'a'); When the input jlkjkljkl When the output jklj , the input jkaljkljkl When the output jk 
 It can also be used when using multidimensional arrays cin.getline(m[i],20) And so on: 
 #include<iostream>
  #include<string>
  using namespace std;   
  main ()
  {
  char m[3][20];
  for(int i=0;i<3;i++)
   {
   cout<<"\n Please enter the first "<<i+1<<" String: "<<endl;
   cin.getline(m[i],20);
   }
  cout<<endl;
  for(int j=0;j<3;j++)
  cout<<" The output m["<<j<<"] The value of the :"<<m[j]<<endl;
  }

Please enter the first string:

[

kskr1

]

Please enter the second string:

[

kskr2

]

Please enter the third string:

[

kskr3

]

Output the value of m[0] :kskr1
Output the value of m[1] :kskr2
Output the value of m[2] :kskr3

4. getline() // accepts 1 string, can receive space and output, including "#include"


#include<iostream>
  #include<string>
  using namespace std;
  main ()
  {
  string str;
  getline(cin,str);
  cout<<str<<endl;
  }

Input: jkljkljkl
Output: jkljkljkl
Input: jkl jfksldfj jklsjfl
Output: jkl jfksldfj jklsjfl

Similar to cin.getline (), but cin.getline () belongs to the istream stream, while getline() belongs to the string stream and are two different functions

[

5, gets() // accept 1 string, can receive space and output, including "#include"

]

 #include<iostream>
  #include<string>
  using namespace std;
  main ()
  {
  char m[20];
  gets(m);  // Can not write m=gets();
  cout<<m<<endl;
  }
[

Input: jkljkljkl
Output: jkljkljkl
Input: jkl jkl jkl
Output: jkl jkl jkl

]

Similar to the example in ES206en.getline (), gets() can also be used in multidimensional arrays:


#include<iostream>
  #include<string>
  using namespace std;
  main ()
  {
  char m[3][20];
  for(int i=0;i<3;i++)
   {
   cout<<"\n Please enter the first "<<i+1<<" String: "<<endl;
   gets(m[i]);
   }
  cout<<endl;
  for(int j=0;j<3;j++)
  cout<<" The output m["<<j<<"] The value of the :"<<m[j]<<endl;
  }

Please enter the first string:

kskr1

Please enter the second string:

kskr2

Please enter the third string:

kskr3

Output the value of m[0] :kskr1
Output the value of m[1] :kskr2
Output the value of m[2] :kskr3

gets() is similar to cin.getline (), except that cin.getline () has one more parameter.

Here helping to show 1, for the purpose of this article in the kskr1, kskr2, kskr3 example, for cin > > It also works because there is no space. If a space is entered, such as "ks kr jkl[enter]", then cin will have received three strings: "ks,kr,jkl"; If "kskr 1[enter]kskr 2[enter]", then receive "kskr,1,kskr"; This is not what we want! cin.getline () and gets() do not generate this error because they can receive Spaces.

conclusion


Related articles: