Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction 
2.
What is the getline function in C++?
3.
Syntax of Getline Function in C++
3.1.
First Syntax
3.2.
Example
3.3.
C++ Code
3.4.
C++ Code
3.5.
Second syntax
3.6.
Example 
3.7.
C++ Code
4.
Getline Character Array 
4.1.
Example
4.2.
C++ Code
5.
Return Type 
6.
Frequently Asked Questions
6.1.
What is the getline function in C++?
6.2.
What is the difference between the cin and getline functions in C++? 
6.3.
How does Getline () work in C?
6.4.
How do you write a Getline function?
7.
Conclusion 
Last Updated: Mar 27, 2024
Easy

Getline Function in C++

Author Sinki Kumari
0 upvote
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction 

The C++ getline() is a common library function that is generally used to read a line or a string from an input stream. And it’s a part of the <string> header. The getline function in C++ helps us to get multiple lines of input from the user. 

getline function in C++

Let’s explore the getline function in C++, its usage, and its role in C++. After reading this blog, you will have a sound understanding of the getline function in C++. 

What is the getline function in C++?

Getline functions in C++ are predefined functions in <string.h> header file. The function is used to get multiple input data lines from the user. It terminates the input data when it encounters a delimiter. 

The cin function also helps to accept the data from the user. The only drawback you face when you use the cin function is that you cannot accept multiple data lines of input from the user. Once you press enter, it treats it as a terminator and does not accept any further input from the user. 

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Syntax of Getline Function in C++

There are two ways by which you can use the getline function in your code. We will explore each way with an example for better understanding. 

First Syntax

  1. In the first syntax, the getline function has three parameters. Let’s understand the meaning of each parameter. 
istream& getline( istream& is, string str, char delim)
  •      is: It is an object of iostream class that informs the function from where to read the input data. 
     
  •      str: It is a string object; the string data read from the input stream gets stored in the string object, i.e., str. 
     
  •     delim: It is a delimiting character. This informs the function to stop accepting input data from the user after reading this character. 
     

Let’s now understand the use of this syntax with an example. 

Example

In the code below, we will use the cin function to understand better how the getline function works. 

  • C++ Code

C++ Code

#include <iostream>

using namespace std;

int main()

{

   string str;

   cout<<"Hey Ninja! Welcome Back. Please write your name"<<endl;

   cin>>str;

   cout<<"Your name is:"<<str;

   return 0;

}

Output

Output

When we see the output, we observe that the user enters his complete name, but only the initial name is printed in the output. When the space is encountered, the cin function does not consider it as the character. To resolve this issue, the getline function is used. It helps to accept multiple data values after space or enter. 

  • C++ Code

C++ Code

#include <iostream>

#include<string.h> 

using namespace std;

int main()

{

   string str;

   cout<<"Hey Ninja! Welcome Back. Please write your name"<<endl;

   cout<<"When input is completed, Press @ : ";

   getline(cin,str,'@');

   cout<<"Your name is:"<<str;

   return 0;

}

Output

Output

In the above code, we have made use of the getline function with the use of a delimiter. Here, we add a delimiter as (@) for the user to end the input. With the use of this function, we can accept multiple values of data from the user. 

Second syntax

The second syntax is almost similar to the first syntax we discussed above. The only difference is that it considers the new line (\n) as a delimiting character. 

The getline function here receives two parameters.

istream& getline( istream& is, string str)

is:-  It is an object of iostream class that informs the function from where to read the input data. 

str:- It is a string object; the string data read from the input stream gets stored in the string object, i.e., str. 

Let's understand the use of this syntax with an example.

Example 

  • C++ Code

C++ Code

#include <iostream>

#include<string.h>

using namespace std;

int main()

{

   string name;

   cout<<"Hey Ninja! Welcome back! Please write your Name:";

   getline(cin,name);

   cout<<"Hello "<<name;

  

   return 0;

}

Output

Output

In the above code, when we press enter after name, the cout hello statement runs. After encountering enter, the code automatically stops receiving data input from the user. So, while using the getline function, it is important to understand when to specifically mention the delimiter character as, by default, it considers a new line as a delimiting character, which can cause a problem when we take multiple lines of input data. 

Getline Character Array 

We have the following syntax to use the getline function in an array. 

istream& getline(char * ptr, int a)

char* ptr: The character pointer that points to an array. 

 int a : The int value is the size until the array accepts the input. This acts as a delimiter, as it won’t accept the data value after crossing the size. 

Let’s write the code for better understanding. 

Example

  • C++ Code

C++ Code

#include <iostream> 

#include <string.h>

using namespace std;



int main()

{

   char name[50];

   cout<<"Enter your name:";

   cin.getline(name, 4);

   cout<<"Your name is :" <<name;

   return 0;

}

Output

 

Output

In the above program, we observe that the size given in the getline function gets considered until that size character gets printed. 

Return Type 

If there is no error and the getline function has successfully run, it returns the reference of the stream from which it reads the input stream. 

The return type of the getline function is not generally used anywhere. 

Frequently Asked Questions

What is the getline function in C++?

The getline function in C++ is a standard library function used to read a line of text from an input stream, such as cin, and store it as a string, including spaces and newline characters, until it encounters a newline character or the specified delimiter.

What is the difference between the cin and getline functions in C++? 

The cin function only accepts a single line character, whereas with getline, you can read multiple lines of character. Cin works for all the data types, whereas getline only works with string data types. 

How does Getline () work in C?

In C, getline() is a standard library function used to read a line of text from an input stream, like stdin, and dynamically allocates memory to store the line, handling resizing as needed. It takes care of buffer management, making it more versatile than fgets().

How do you write a Getline function?

To write a custom getline function in C, you can use a loop to read characters from an input stream, dynamically allocate memory, and resize the buffer as necessary while handling newline or EOF conditions. The function returns the dynamically allocated line.

Conclusion 

The article discussed various aspects of the getline function in C++. We started with the introduction and later looked into the syntax, declaration, and examples for a better understanding of the getline function in C++. We hope this blog has helped you enhance your knowledge regarding the getline function in C++. 

To learn more about C++ and its functions, please refer

You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. 

Previous article
C++ perror()
Next article
Inline functions
Live masterclass