Introduction to C++ getline()
The getline() function of C++ used to take the user input in multiple lines until the delimiter character found. The getline() function is predefine function whose definition is present in a <string.h> header file, so to use getline() function in a program, the first step is to include the <string.h> header file. We know that already we have cin object to accept the input from the user but the cin object does not allow we to accept the user input in multiple lines, so to accept input from the input stream in multiple lines or a string until delimiter character is found we can use the getline() function.
The Syntax of C++ getline( ) function
The getline( ) function can be represented in two ways based on the number of parameters they can accept, in other words, based on the parameters passes to the getline( ) function the particular getline( ) function overload. Both representations are –
1. istream& getline( istream& is, string& str, char delim );
The above is a first representation where it accepts three parameters which are is, str, and delim.
Parameters –
- is – is parameter is an istream class object which represents from where to read the input string.
- str – str parameter represents a string object where input is to be stored after accepting from the input stream.
- delim – delim parameter represents delimiting character until where the input string to be accepted.
The return value of the getline( ) function is the object of the input stream class that is ‘is’ itself which is accepted as a parameter to the function.
2. istream& getline( istream& is, string& str );
The above is a second representation where it accepts two parameters which are is and str. It does not accept delim parameters and the other two parameters are similar to the first representation.
Working and Examples of the getline( ) function in C++
Next, we write the C++ code to understand the getline( ) function working more clearly with the following example where we use getline( ) function to accept the input from the user, as below –

4.5 (8,448 ratings)
View Course
Example #1
Code:
#include <iostream>
using namespace std;
#include<string.h>
int main()
{
// declaration of variable
string message;
cout << "Please enter your message : " ;
// accept input by using getline( ) function
getline( cin, message );
// display accepted message
cout<< "Your message is = "<<message;
return 0;
}
Output:
As in the above code, the getline() function accepted the full string even as there are spaces present between the characters.
Next, we rewrite the above C++ code to understand how could be the input accepted if we do not use the getline( ) function. So in the next code, we accept the input using getline( ) function, as below –
Example #2
Code:
#include <iostream>
#include<string.h>
using namespace std;
int main()
{
// declaration of variable
string message;
cout << "Please enter your message : " ;
// accept input
cin>>message;
// display accepted message
cout<< "Your message is = "<<message;
return 0;
}
Output:
As in the above code, we try to accept a by using cin object instead of using getline() function and we see in the output that the cin object accepted the input until the first space is found. So to accept entire string or multiple lines of string we need to use getline() function of C++.
Next, we write the C++ code to understand the getline( ) function working delimiter character more clearly with the following example where we use getline( ) function to accept the input until delimiter character is found from the user as below –
Example #3
Code:
#include <iostream>
using namespace std;
#include<string.h>
int main()
{
// declaration of variable
string message;
cout << "Please enter your message : " ;
// accept input
getline( cin, message, ' ' );
// display accepted message
cout<< "Your message is = "<<message;
return 0;
}
Output:
As in the above code, the getline() function is used to accept an input but now the third parameter delimiter character is passed as space(‘ ‘) and hence the getline() function accepts the input stream until space is found the characters present after space is ignored.
Next, we write the C++ code where we delimiter character will be ‘r’, as below –
Example #4
Code:
#include <iostream>
using namespace std;
#include<string.h>
int main()
{
// declaration of variable
string message;
cout << "Please enter your message : " ;
// accept input
getline( cin, message, 'r' );
// display accepted message
cout<< "Your message is = "<<message;
return 0;
}
Output:
So in the above code, the getline() function accepted an input until the delimiter character ‘r’ is found.
Next, we write the C++ code to use the getline( ) function for character array with different syntax that is istream& getline(char* , int size) where char* is character pointer points to array and size is delimiter which specifies to accept input in an array until that size reached. Note that this syntax accepts until space or specified sized whichever found first.
Example #5
Code:
#include <iostream>
using namespace std;
#include<string.h>
int main()
{
// declaration of variable
char message[5];
cout << "Please enter your message : " ;
// accept input
cin.getline( message, 5 );
// display accepted message
cout<< "Your message is = "<<message;
return 0;
}
Output:
Conclusion
The getline() function in C++ is a built-in function defined in <string.h> header file and it is used to accept the user input in multiple lines until the delimiter character found.
Recommended Articles
This is a guide to C++ getline(). Here we discuss the Working and Examples of the getline( ) function in C++ along with the syntax. You may also have a look at the following articles to learn more –