The C++ getline() is a standard library function that is used to read a string or a line from an input stream. It extracts the characters from the input stream and appends it to the given string object until the delimiting character is encountered.
Example:
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
string name;
// Taking input from cin stream(standard
// input stream) and storing it in name
getline(cin, name);
cout << name;
return 0;
}
Output
Geeks (Enter By user)
Geeks
Syntax of getline()
The std::getline() function is defined inside <string> header file in C++.
C++
getline(stream, str, delim);
Parameters:
- stream: Input stream from where characters are read.
- str: String where the input is stored after being read from the stream.
- delim(optional): Delimiter is a character where getline stop to read the data. By default, newline character '\n' is the delimiter.
Return Value:
- It returns the reference to the input stream.
Examples of getline() Function
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().
C++
#include <iostream>
#include <string>
using namespace std;
int main() {
string str;
// Read input from cin
getline(cin, str);
cout << "Hello, " << str
<< " welcome to GFG !";
return 0;
}
Output
Harsh Agarwal (Enter by user)
Hello, Harsh Agarwal welcome to GFG !
Tokenize a String
We can use getline() function along with stringstream to split a sentence on the basis of a character.
CPP
#include <bits/stdc++.h>
using namespace std;
int main() {
string S, T;
getline(cin, S);
// Store the string S
// into stringstream X
stringstream X(S);
while (getline(X, T, ' '))
cout << T << endl;
return 0;
}
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.
CPP
// C++ program to demonstrate
// anomaly of delimitation of
// getline() function
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
int id;
// Taking id as input
cout << "Please enter your id: \n";
cin >> id;
// Takes the empty character as input
cout << "Please enter your name: \n";
getline(cin, name);
// Prints id
cout << "Your id : " << id << "\n";
// Prints nothing in name field
// as "\n" is considered a valid string
cout << "Hello, " << name
<< " welcome to GfG !\n";
// Again Taking string as input
getline(cin, name);
// This actually prints the name
cout << "Hello, " << name
<< " welcome to GfG !\n";
return 0;
}
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 !