C Programming and Data Structures (48 Blogs)

How To Implement Getline In C++?

Last updated on Aug 08,2019 15 Views
How To Implement Getline In C++?

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-mobile-banner-bg

myMock Interview Service for Real Tech Jobs

  • Mock interview in latest tech domains i.e JAVA, AI, DEVOPS,etc
  • Get interviewed by leading tech experts
  • Real time assement report and video recording

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.

Following points will be covered in this article,

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:

Output-Getline in C++-Edureka

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.

Comments
0 Comments

Browse Categories

Subscribe to our Newsletter, and get personalized recommendations.