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.
There are two ways of representing a function.
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 valueThis function returns the input stream object, which is passed as a parameter to the function.
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 valueThis 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.
#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.
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.
#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.
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:
#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.
We can also define the getline() function for character array, but its syntax is different from the previous one.
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.
#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; }