getline (string) in C++
The C++ getline() is a standard library function that is used to read a string or a line from an input stream. It is a part of the <string> header. The getline() function extracts characters from the input stream and appends it to the string object until the delimiting character is encountered. While doing so the previously stored value in the string object str will be replaced by the input string if any.
The getline() function can be represented in two ways:
- Syntax:
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.
-
Syntax:
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 does not accept any delimitation character. This function considers a new line or (‘\n’) character as the delimitation 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.
Below program demonstrates the working of the getline() function
Example 1:
// C++ program to demonstrate getline() function #include <iostream> #include <string> using namespace std; int main() { string str; cout << "Please enter your name: \n"; getline(cin, str); cout << "Hello, " << str << " welcome to GfG !\n"; return 0; } |
Input:
Harsh Agarwal
Output:
Hello, Harsh Agarwal welcome to GfG!
Example 2: We can use getline() function to split a sentence on the basis of a character. Let’s look at an example to understand how it can be done.
// C++ program to understand the use of getline() function #include <bits/stdc++.h> using namespace std; int main() { string S, T; getline(cin, S); stringstream X(S); while (getline(X, T, ' ')) { cout << T << endl; } return 0; } |
Input:
Hello, Faisal Al Mamun. Welcome to GfG!
Output:
Hello, Faisal Al Mamun. Welcome to GfG!
Related Articles:
- How to use getline() in C++ when there are blank lines in input?
- getline() function and character array
This article is contributed by Harsh Agarwal and improved by Faisal Al Mamun. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- How to use getline() in C++ when there are blank lines in input?
- getline() function and character array
- std::string::append vs std::string::push_back() vs Operator += in C++
- Check if a string can be formed from another string using given constraints
- std::string::remove_copy(), std::string::remove_copy_if() in C++
- std::string::replace_copy(), std::string::replace_copy_if in C++
- std::string::replace , std::string::replace_if in C++
- string at() in C++
- Tokenizing a string in C++
- std::string::data() in C++
- std::string::append() in C++
- std::string::assign() in C++
- std::string::find_last_not_of in C++
- std::string::push_back() in C++
- std::string::find_first_not_of in C++



