C++ cin.getline and getline of

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

Let's first look at the usage of C++ cin.getline is as follows:

Another way in which C++ character arrays can be used differently than string objects is that they must be handled using a different set of functions. For example, to read 1 line of input, you must use the cin.getline function instead of the getline function. These two names look similar, but they are two different functions that are not interchangeable.

Like getline 1, ES14en.getline allows you to read strings that contain whitespace. It continues to read until it reads the maximum number of characters specified, or until the enter key is pressed. Here is an example of its use:

cin.getline(sentence, 20);

The getline function takes two comma-separated arguments. The first argument is the name of the array to store the string. The second parameter is the size of the array. When the ES23en. getline statement executes, cin reads 1 fewer characters than that number, making room for the null terminator. This eliminates the need to use the setw operator or the width function. The above statement reads up to 19 characters, and the null terminator is automatically placed after the last 1 character of the array.

The following program demonstrates the use of the getline function, which can read up to 80 characters:


// This program demonstrates cinT s getline function
// to read a line of text into a C-string.
#include <iostream> , 
using namespace std;
int main()
{
  const int SIZE = 81;
  char sentence[SIZE];
  cout << "Enter a sentence: ";
  cin.getline (sentence, SIZE);
  cout << "You entered " << sentence << endl;
  return 0;
}

Program output results:

[

Enter a sentence: To be, or not to be, that is the question.
You entered To be, or not to be, that is the question.

]

Supplement: two USES of C++ getline()

getline() : Used to read in 1 whole row. In C++, there are two types of getline functions. The first is defined in the header file < istream > Is a member function of the istream class; The second is defined in the header file < string > , is a normal function.

Type 1: In < istream > The getline() function in


istream& getline (char* s, streamsize n );
istream& getline (char* s, streamsize n, char delim );

The n function reads up to n characters from istream, including the closing tag, and stores them in the array corresponding to s. Even if n characters are not read enough, the read terminates if the delim identifier or word limit is reached. The delim identifier is read, but not saved into the array corresponding to s. Note that the delim identifier is valid only when the maximum number of characters n is specified.


#include <iostream>
using namespace std;

int main()
{
 char name[256], wolds[256];
 cout<<"Input your name: ";
 cin.getline(name,256);
 cout<<name<<endl;
 cout<<"Input your wolds: ";
 cin.getline(wolds,256,',');
 cout<<wolds<<endl;
 cin.getline(wolds,256,',');
 cout<<wolds<<endl;
 return 0;
}

The input

[

Kevin
Hi,Kevin,morning

]

The output

[

Kevin
Hi
Kevin

]

Type 2: In < string > The getline function has four overloaded forms:


istream& getline (istream& is, string& str, char delim);
istream& getline (istream&& is, string& str, char delim);
istream& getline (istream& is, string& str);
istream& getline (istream&& is, string& str);

The usage is similar to the first one above, but the istream read is passed into the function as the parameter is. The read string is stored in str of type string.

is: Represents 1 input stream, such as cin.

str: A reference of type string used to store flow information in an input stream.

delim: variable of type char, with truncated character set; If '\n' is encountered without custom Settings, the input is terminated.


#include<iostream>
#include<string>
using namespace std;
int main(){
 string str;
 getline(cin, str, 'A');
 cout<<"The string we have gotten is :"<<str<<'.'<<endl;
 getline(cin, str, 'B');
 cout<<"The string we have gotten is :"<<str<<'.'<<endl;
return 0;}

The input

[

i_am_A_student_from_Beijing

]

The output

[

The string we have gotten is :i_am_.
The string we have gotten is :_student_from_.

]

conclusion


Related articles: