![]() |
VOOZH | about |
getline() in C++ is a standard library function used to read an entire line of text from an input stream.
It stores the extracted characters into a string until a delimiter character is encountered.
Output
Geeks (Enter By user)
Geeks
The std::getline() function is defined inside <string> header file in C++.
getline(stream, str, delim);
Parameters
Return Value
The following examples demonstrate the use of getline function to input from different streams in C++
If we try to read the space separated string from cin stream using >> operators, it only reads the first word. Because whitespace character ' ' is the delimiter for cin. But thats not the case with getline().
Output
Harsh Agarwal (Enter by user)
Hello, Harsh Agarwal welcome to GFG !
We can use getline() function along with stringstream to split a sentence on the basis of a character.
Output
Hello Students, Welcome to GFG! (Enter by user)
Hello
Students,
Welcome
to
GFG!
In the above example, we have used getline() to read strings from the stringstream X and did it till EOF is reached. In each read, characters until the white space (individual words) are stored in the string and printed. Then the remaining words are also read in the same way.
Output
Please enter your id:
10 (press enter)
Please enter your name:
Your id : 10
Hello, welcome to GfG !
Geek (Enter your name)
Hello, Geek welcome to GfG !