How to Use Getline in C++
What is getline in C++?
Similar to the cin function, which allows the programmer to take input from the user getline() function also takes input from the user. But in this case, unlike the cin function, which takes only one line from the user, the getline function will take multiple lines from the user. Moreover, similar to the cin function, getline function is also a part of the iostream.h header file.
The syntax for the getline function:
We can declare the getline function in two ways:
- istream& getline( istream& is, string& str );
- istream& is: It is used to read the input.
- Istream& str: It stores the input in a variable.
- istream& getline (istream& is, string& str, char delimiting);
- istream& is: It is used to read the input.
- Istream& str: It stores the input in a variable.
- Char delimiting: This character will stop taking input when specific characters are encountered.
Examples
Now let's see a few examples of the getline function:
Before looking into the getline function example, let's see how the cin function operates
#include <iostream>
using namespace std;
// Driver Code
int main()
{
int i;
// Take input using cin
cin >> i;
// Print output
cout << i;
return 0;
}
Output:

Explanation:
The above code is written to demonstrate the usage of the cin function. In the above code, we have created an integer variable i, then using the cin function, we took the input from the user as we are using the cin function, so we need to include the iostream header file in the code. We can see that using the cin function, we have taken only a single line of input.
Now let's see how we can take multiple input lines using the getline function.
Example-1:
#include <iostream>
#include<string.h>
using namespace std;
int main()
{
string name; // variable declaration.
std::cout << "Enter your name :" << std::endl;
getline(cin,name); // implementing a getline() function
cout<<"\nHello "<<name;
return 0;
}
Output:

Explanation:
The above code is written to demonstrate the usage of the getline function. In the above code, we have declared a string name. Then using the getline function, we have taken the input from the user. The main use of the getline function is that it can take multiple lines of input. In the next example, let's see how the getline function reacts when we give multiple lines of input and how the cin function reacts when we give multiple lines of input.
Example-2
#include <iostream>
#include<string.h>
using namespace std;
int main()
{
string sen; // variable declaration.
std::cout << "Enter any sentence :" << std::endl;
cin>>sen; // implementing a cin function
cout<<"\nHello "<<sen;
return 0;}
Output:

Explanation:
This example demonstrates how a cin function behaves when we give multiple lines of input. In the above code, we have declared a variable sen. Using the cin function, we have given the input. We can observe that we have multiple lines of input in the output, but the cin function has only taken the first word. So here we can understand that the cin function will only be the input until we give space.
Example-3
Now let's see how the getline function reacts when we give multiple lines of input:
#include <iostream>
#include<string.h>
using namespace std;
int main()
{
string sen; // variable declaration.
std::cout << "Enter any sentence :" << std::endl;
getline(cin,sen); // implementing a cin function
cout<<"\nHello "<<sen;
return 0;}
Output:

Explanation:
The above code is written to demonstrate how the getline function will react when given multiple lines of input. In the code, we have declared a variable set of string types then, using the getline function, we have taken the input. In the output, we can observe that we have given multiple input lines. Using the getline fucntion, all the lines are stored in the variable. So when we printed the output, the input given appeared.
Example-4:
#include <iostream>
#include<string.h>
using namespace std;
int main()
{
string name; // variable declaration
std::cout << "Enter your name :" << std::endl;
getline(cin,name,' '); // implementing getline() function with a delimiting character.
cout<<"\nname is :"<<name;
}
Output:

Explanation:
While discussing the syntax for the getline function, we have seen that we can give the delimiting character in the parameter. Here in the code, we have given space as the delimiting character so the getline function will stop taking the input when the space occurs in the input. In the output, we can see that after the example word, we have given space, so the getline function stopped taking the input, which we can see when we printed the variable.