×

How to Use Getline in C++

What is getline in C++?

Similar to the cin function, which allows the programmer to take input from the user getline() function also takes input from the user. But in this case, unlike the cin function, which takes only one line from the user, the getline function will take multiple lines from the user. Moreover, similar to the cin function, getline function is also a part of the iostream.h header file.

The syntax for the getline function:

We can declare the getline function in two ways:

  1. istream& getline( istream& is, string& str );  
    • istream& is: It is used to read the input.
    • Istream& str: It stores the input in a variable.
  2. istream& getline (istream& is, string& str, char delimiting);
    • istream& is: It is used to read the input.
    • Istream& str: It stores the input in a variable.
    • Char delimiting: This character will stop taking input when specific characters are encountered.

Examples

Now let's see a few examples of the getline function:

Before looking into the getline function example, let's see how the cin function operates

#include <iostream>
using namespace std;


// Driver Code
int main()
{
    int i;


    // Take input using cin
    cin >> i;


    // Print output
    cout << i;


    return 0;
}

Output:

How to Use Getline In C++

Explanation:

The above code is written to demonstrate the usage of the cin function. In the above code, we have created an integer variable i, then using the cin function, we took the input from the user as we are using the cin function, so we need to include the iostream header file in the code. We can see that using the cin function, we have taken only a single line of input.

Now let's see how we can take multiple input lines using the getline function.

Example-1:

#include <iostream>
#include<string.h>
using namespace std;
int main()
{
string name; // variable declaration.
std::cout << "Enter your name :" << std::endl;
getline(cin,name); // implementing a getline() function
cout<<"\nHello "<<name;
return 0;
}

Output:

How to Use Getline In C++

Explanation:

The above code is written to demonstrate the usage of the getline function. In the above code, we have declared a string name. Then using the getline function, we have taken the input from the user. The main use of the getline function is that it can take multiple lines of input. In the next example, let's see how the getline function reacts when we give multiple lines of input and how the cin function reacts when we give multiple lines of input.

Example-2

#include <iostream>
#include<string.h>
using namespace std;
int main()
{
string sen; // variable declaration.
std::cout << "Enter any sentence :" << std::endl;
cin>>sen; // implementing a cin function
cout<<"\nHello "<<sen;
return 0;}

Output:

How to Use Getline In C++

Explanation:

This example demonstrates how a cin function behaves when we give multiple lines of input. In the above code, we have declared a variable sen. Using the cin function, we have given the input. We can observe that we have multiple lines of input in the output, but the cin function has only taken the first word. So here we can understand that the cin function will only be the input until we give space.

Example-3

Now let's see how the getline function reacts when we give multiple lines of input:

#include <iostream>
#include<string.h>
using namespace std;
int main()
{
string sen; // variable declaration.
std::cout << "Enter any sentence :" << std::endl;
getline(cin,sen); // implementing a cin function
cout<<"\nHello "<<sen;
return 0;}

Output:

How to Use Getline In C++

Explanation:

The above code is written to demonstrate how the getline function will react when given multiple lines of input. In the code, we have declared a variable set of string types then, using the getline function, we have taken the input. In the output, we can observe that we have given multiple input lines. Using the getline fucntion, all the lines are stored in the variable. So when we printed the output, the input given appeared.

Example-4:

#include <iostream>
#include<string.h>
using namespace std;
int main()
{
string name; // variable declaration
std::cout << "Enter your name :" << std::endl;
getline(cin,name,' '); // implementing getline() function with a delimiting character.
cout<<"\nname is :"<<name;
}

Output:

How to Use Getline In C++

Explanation:

While discussing the syntax for the getline function, we have seen that we can give the delimiting character in the parameter. Here in the code, we have given space as the delimiting character so the getline function will stop taking the input when the space occurs in the input. In the output, we can see that after the example word, we have given space, so the getline function stopped taking the input, which we can see when we printed the variable.


Related Topics

C++ 11 vs C++ 14 vs C++ 17

C++ is a language that is used to create a high-performance application. C++ 11, C++ 14, and C++ 17 are the different version of C++. There are some differences between...

1 minute read.

Precision of floating point numbers Using these functions floor(), ceil(), trunc(), round() and setprecision() in C++

Precision of floating point numbers Using these functions floor(), ceil(), trunc(), round() and setprecision() in C++ 1/2 decimal equal is 0.555555555555555555555 .... An indefinite number of lengths will require the storage...

4 minutes read.

abs() function in C++

In C++, the abs() function returns the absolute value of any integer number. Using this function a negative integer is multiplied by -1 and positive number or zero is returned...

4 minutes read.

Initialization of Data Members

In this tutorial, we'll look at how to initialise static member variables in C++. Static members, such as functions or variables, can be added to C++ classes. After declaring the...

1 minute read.

ATM machine program in C++ using functions

Automated Teller Machines (ATMs) carry out daily financial transactions. They are straightforward and simple, allowing customers to complete self-service transactions quickly. ATMs can then be used to withdraw cash, deposit...

3 minutes read.

Passing a Vector to a function in C++

A pointer is sent to the function when we feed it an array. However, there are two ways to pass a vector: Pass by Value Pass by Reference A copy of a vector...

3 minutes read.

Tensorflow in C++

With cppflow, you can execute TensorFlow models in C++ without Bazel, TensorFlow installation, or TensorFlow compilation. Tensor manipulation, eager execution, and running stored models straight from C++ are all possible. Knowing...

7 minutes read.

C++ Keywords

In this article, we will discuss keywords in C++ with their several features and functions. What are Keywords in C++? In C++, a keyword is a reserved word that has a predefined...

4 minutes read.

Web Development in C++

Before learning above C++ web development, we need to learn about CGI What is CGI? CGI stands for common gateway interface. CGI is a standard that tells us how the exchange of...

4 minutes read.

Reverse function in C++

The function std::reverse() is included in the standard template library of C++. It takes in a beginning and ending iterator, reversing the order. To use the reverse statement, we need...

2 minutes read.

Initialize Array of objects with parameterized constructors in C++

Initialize Array of objects with parameterized constructors in C++ When a class is defined, only the specification for the object is specified; no memory or capacity is allocated. You need to...

3 minutes read.

4-Dimensional Array in C/C++

A four-dimensional (4D) array is an array of three-dimensional (3D) arrays, or in other words we can say that a 4- dimensional array is an array of arrays of arrays...

3 minutes read.

C++ File Handling

File handling is a mechanism that manipulates the data stored in files. File handling store output data from the program to external file and read file data to the program. There...

3 minutes read.

C++ Encapsulation

The following two essential components are present in all C++ programmes: Functions are the parts of a programme that perform actions, and they are termed programme statements (code).Program data is the...

4 minutes read.

Array of Vectors in C++ STL

Prerequisites: C++ STL Arrays and C++ STL Vector. A group of items kept in consecutive memory region is known as an array. It is to group similar objects of the same...

4 minutes read.

Loops in C++

A loop statement in most programming languages allows us to execute a statement or a collection of statements numerous times. Control structures of programming languages vary, allowing for more complex...

6 minutes read.

Converting string into integer in C++

When programming in C++, we'll frequently need to change one data type to another. When we use C++ to create apps, we must transform data from one type to another. When...

7 minutes read.

Continue in C++ While loop

Continue statement: Inside the loop, the continue statement is used to control the loop. C++ utilizes the continue keyword to implement the continue statement, which transfers the program's flow at the...

4 minutes read.

INT_MAX and INT_MIN in C/C++

In competitive programming, assigning a variable that maximum or minimum value a data type can carry is frequently necessary. Still, it might be challenging to recall such a significant, exact...

3 minutes read.

C++ Recursion Function

A programming method called recursion that uses a function to call itself to address lesser problems. The Fibonacci sequence, factorial computation, and tree traversal are just a few of the...

4 minutes read.