getline() in C++ is a special convenience function provided by string class for reading line by line.
This function reads all characters, including leading whitespaces, until the line delimiter or
end-of-file is reached. The line delimiter is extracted but not appended. By default, the line delimiter is the newline character, but you can pass your own “line” delimiter as an optional argument.This way, you can read token by token, separated by any arbitrary character:
Syntax 1:
istream& getline(istream& is, string& str, char delim);
Parameters:
- is: It is an object of istream class and tells the function about the stream from where to read the input from.
- str: It is a string object, the input is stored in this object after being read from the stream.
- delim: It is the delimitation character which tells the function to stop reading further input after reaching this character.
Return Value: This function returns the same input stream as is which is accepted as parameter.
Example:
std::string s;
while (getline(std::cin,s)) { // for each line read from cin
…
}
Syntax 2:
istream& getline (istream& is, string& str);
The second declaration is almost the same as that of the first one. The only difference is, the latter have an delimitation character which is by default new line(\n)character.
Parameters:
- is: It is an object of istream class and tells the function about the stream from where to read the input from.
- str: It is a string object, the input is stored in this object after being read from the stream.
Return Value: This function returns the same input stream as is which is accepted as parameter.
Example:
std::string s;
while (getline(std::cin,s,’:’)) { // for each token separated by ’:’
…
}
Note that if you read token by token, the newline character is not a special character. In this case,
the tokens might contain a newline character.
Note also that since C++11, getline() is overloaded for both lvalue and rvalue stream references, which allows using temporary string streams:
void process (const std::string& filecontents)
{
// process first line of passed string:
std::string firstLine;
std::getline(std::stringstream(filecontents), // OK since C++11
firstLine);
…
}
Let’s see some example usage:
#include <iostream> #include <string> using namespace std; int main() { string str; cout << "Please enter your name: \n" getline(cin, str); cout << "Hello, " << str <<" \n" return 0; } Input: techtrending Output: Hello techtrending
Example 2:
Using getline() function to split a sentence on the basis of delimeter
// C++ program to understand the use of getline() function #include<iostream> #include <sstream> using namespace std; int main() { string str, str2; getline(cin, str); stringstream sstr(str); while (getline(sstr, str2, ',')) { cout << str2 << endl; } return 0; } Input: trend,tech,way Output: trend tech way
Problem when Mixing cin and getline functionality:
It is worthwhile to point out problems that can arise if we have code that mixes both cin and getline. getline() removes the newline from the input stream while cin does not. This does not cause any problems if our program uses all cin’s, because cin will ignore leading whitespace. But since getline does not ignore the leading whitespace, it can lead to undesirable results, namely empty strings
#include <iostream> #include <string> using namespace std; int main() { string name; int RollNo; // Taking id as input cout << "Please enter your RollNo: \n" cin >> RollNo; // Takes the newline character as input cout << "Please enter your name: \n" getline(cin, name); // Prints RollNo cout << "Your RollNo : " << RollNo << "\n" // Prints nothing in name field // as "\n" is considered a valid string cout << "Hello, " << name << " This is techtrending !\n" return 0; } Output: Please enter your RollNo: 1234 Please enter your name: Your RollNo: 1234 Hello, This is learntechway!
The issue is because cin
leaves an end of line, \n
, character which is then read by getline()
. In the line “cin>> RollNo” we input the letter ‘1234’ into variable RollNo. However, the newline when we hit the carriage return is left in the input stream. If we use another cin, this newline is considered whitespace and is ignored. However, if we use getline, this is not ignored. Instead we read a newline into the second getline, and exit. A blank string has been read into name in the getline.To Overcome this you can use cin.ignore() after you use “cin>>RollNo” to discard the next value(newline character)