Getline Library Function In C++ | Edureka
Maybe your like
Handling strings is crucial part when it comes to programming. In this article we will learn about Getline in C++ a standard library function that lets you do wonders with strings.
Table of Content- Getline In C++
- Sample Code Syntax
- Getline character array
Following points will be covered in this article,
- Getline function
- Sample Code Syntax
- Getline character array
Moving on with this article on Getline in C++
Getline In C++
While using C++, std::cin does not support accepting multiple lines in one go, to do this we have some in-built functions like getline. To accept a string or a line of input stream as input, we have an in-built function called getline(). This function is under the <string> header file.
It accepts all the strings until a newline character is encountered.
Syntax:
There are 2 ways to use a getline() function:
istream& getline (istream& is, string& str, char delim);The “is” is an object of the stream class. Where to read the input stream from is told to the function by this “is” object. str is the string object where the string is stored. delim is the delimiting character.
Example
getline (cin, str,”hi”);In this example, the getline function will read until the new line character is found.
The second way,
istream& getline (istream& is, string& str);This is the same as the above syntax but we do not have a delimiting character
Example
getline (cin, str);In this example, the getline function will read all the input the user has to input. This was is widely preferred by the programmers.
Moving on with this Getline in C++ article
Sample Code Syntax
#include <iostream> #include <string> using namespace std; int main () { string s; cout << "Please enter a string: n"; getline (cin, s); cout << "The Input string you have entered is,"<<endl<<s; return 0; }Output:

Moving on with this Getline in C++ article
Getline character array
For character array, we use a slightly different syntax,
istream& getline(char*, int size)We have a size of the array as a delimiter and the input can’t cross that size.
Example
cin.getline(str, 20);We call the getline function by using the instream object called cin. We pass the length of the array. This is mainly used when we have a char array with a specified size.
Thus we have come to an end of this article on ‘Getline in C++’. If you wish to learn more, check out the Java Training by Edureka, a trusted online learning company. Edureka’s Java J2EE and SOA training and certification course is designed to train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.
Got a question for us? Please mention it in the comments section of this blog and we will get back to you as soon as possible.
Tag » How To Use Getline In C++
-
Getline In C++ - GeeksforGeeks
-
An Introduction To C++ Getline With Syntax And Examples - Simplilearn
-
Std::getline - C++
-
How To Use Std::getline() In C++? - DigitalOcean
-
Getline In C++ – Cin Getline() Function Example - FreeCodeCamp
-
C++ Getline() - Javatpoint
-
11.2: C++ Input- Getline() - Engineering LibreTexts
-
Std::getline
-
Learn How To Use Getline C++ String In No Time - BitDegree
-
C++ Getline() | Learn The Examples Of The Getline( ) Function In C++
-
Getline C++ Explained With Examples - Udacity
-
C++ Program To Read String Using tline() - TutorialAndExample
-
C++ Program To Read String Using tline()
-
C++ Getline Function | Reading An Entire Line From Streams - YouTube