Uses of the getline( ) function in C++

Uses of the getline( ) function in C++
Techiio-author
Written by Sagar RabidasFebruary 10, 2022
6 min read
C++
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Techiio-author
Sagar Rabidas

Software Developer

In this blog, we will discuss uses of the getline( ) function in C++

C++ getline()

The getline() function of C++ is used to take the user input in multiple lines until the delimiter character is found. The getline() function is predefine function whose definition is present in a <string.h> header file, so to use getline() function in a program, the first step is to include the <string.h> header file. We know that already we have an object to accept the input from the user but the c in the object does not allow us to accept the user input in multiple lines, so to accept input from the input stream in multiple lines or a string until delimiter character is found we can use the getline() function.

The Syntax of C++ getline( ) function

The getline( ) function can be represented in two ways based on the number of parameters they can accept, in other words, based on the parameters passing to the getline( ) function the particular getline( ) function overload. Both representations are –

istream& getline( istream& is, string& str, char delim );

The above is a first representation where it accepts three parameters which are is, str, and delim.

Parameters –

  • is – is parameter is an instream class object which represents from where to read the input string.
  • str – str parameter represents a string object where input is to be stored after accepting from the input stream.
  • delim – delim parameter represents delimiting character until where the input string to be accepted.

The return value of the getline( ) function is the object of the input stream class that is ‘is’ itself which is accepted as a parameter to the function.

istream& getline( istream& is, string& str );  

The above is a second representation where it accepts two parameters which are is and str. It does not accept delim parameters and the other two parameters are similar to the first representation.

Working and Examples of the getline( ) function in C++

Next, we are writing the C++ code to understand the getline( ) function working more clearly with the following example where we use the getline( ) function to accept the input from the user, as below –

Example #1

Code:

#include <iostream>
using namespace std;
#include<string.h>
int main()
{
// declaration of variable
string message;
cout << "Please enter your message : " ;
// accept input by using getline( ) function
getline( cin, message );
// display accepted message
cout<< "Your message is = "<<message;
return 0;
}

Example #2

Code:

#include <iostream>
#include<string.h>
using namespace std;
int main()
{
// declaration of variable
string message;
cout << "Please enter your message : " ;
// accept input
cin>>message;
// display accepted message
cout<< "Your message is = "<<message;
return 0;
}

Example #3

Code:

#include <iostream>
using namespace std;
#include<string.h>
int main()
{
// declaration of variable
string message;
cout << "Please enter your message : " ;
// accept input
getline( cin, message, ' ' );
// display accepted message
cout<< "Your message is = "<<message;
return 0;
}

Example #4

Code:

#include <iostream>
using namespace std;
#include<string.h>
int main()
{
// declaration of variable
string message;
cout << "Please enter your message : " ;
// accept input
getline( cin, message, 'r' );
// display accepted message
cout<< "Your message is = "<<message;
return 0;
}

Example #5

Code:

#include <iostream>
using namespace std;
#include<string.h>
int main()
{
// declaration of variable
char message[5];
cout << "Please enter your message : " ;
// accept input
cin.getline( message, 5 );
// display accepted message
cout<< "Your message is = "<<message;
return 0;
}

C
C++
getline( ) function
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance