C++ getline()

The cin is an object which is used to take input from the user but does not allow to take the input in multiple lines. To accept the multiple lines, we use the getline() function. It is a pre-defined function defined in a <string.h> header file used to accept a line or a string from the input stream until the delimiting character is encountered.

Syntax of getline() function:

There are two ways of representing a function:

  • The first way of declaring is to pass three parameters.
snippet
istream& getline( istream& is, string& str, char delim );

The above syntax contains three parameters, i.e., is, str, and delim.

Where,

is: It is an object of the istream class that defines from where to read the input stream.

str: It is a string object in which string is stored.

delim:

Return value

This function returns the input stream object, which is passed as a parameter to the function.

  • The second way of declaring is to pass two parameters.
snippet
istream& getline( istream& is, string& str );

The above syntax contains two parameters, i.e., is and str. This syntax is almost similar to the above syntax; the only difference is that it does not have any delimiting character.

Where,

is: It is an object of the istream class that defines from where to read the input stream.

str: It is a string object in which string is stored.

Return value

This function also returns the input stream, which is passed as a parameter to the function.

Let's understand through an example.

First, we will look at an example where we take the user input without using getline() function.

snippet
#include <iostream>
#include<string.h>
using namespace std;
int main()
{
string name; // variable declaration
std::cout << "Enter your name :" << std::endl;
cin>>name;
cout<<"\nHello "<<name;
return 0;
}

In the above code, we take the user input by using the statement cin>>name, i.e., we have not used the getline() function.

Output

Output
Enter your name : John Miller Hello John

In the above output, we gave the name 'John Miller' as user input, but only 'John' was displayed. Therefore, we conclude that cin does not consider the character when the space character is encountered.

Let's resolve the above problem by using getline() function.

snippet
#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;}

In the above code, we have used the getline() function to accept the character even when the space character is encountered.

Output

Output
Enter your name : John Miller Hello John Miller

In the above output, we can observe that both the words, i.e., John and Miller, are displayed, which means that the getline() function considers the character after the space character also.

When we do not want to read the character after space then we use the following code:

snippet
#include <iostream>
#include<string.h>
using namespace std;
int main()
{
string profile; // variable declaration
std::cout << "Enter your profile :" << std::endl;
getline(cin,profile,' '); // implementing getline() function with a delimiting character.
cout<<"\nProfile is :"<<profile;
}

In the above code, we take the user input by using getline() function, but this time we also add the delimiting character('') in a third parameter. Here, delimiting character is a space character, means the character that appears after space will not be considered.

Output

Output
Enter your profile : Software Developer Profile is: Software

Getline Character Array

We can also define the getline() function for character array, but its syntax is different from the previous one.

Syntax

snippet
istream& getline(char* , int size);

In the above syntax, there are two parameters; one is char*, and the other is size.

Where,

char*: It is a character pointer that points to the array.

Size: It acts as a delimiter that defines the size of the array means input cannot cross this size.

Let's understand through an example.

snippet
#include <iostream>
#include<string.h>
using namespace std;
int main()
{
char fruits[50]; // array declaration
cout<< "Enter your favorite fruit: ";
cin.getline(fruits, 50); // implementing getline() function
std::cout << "\nYour favorite fruit is :"<<fruits << std::endl;
return 0;
}

Output

Output
Enter your favorite fruit: Watermelon Your favorite fruit is: Watermelon
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents