Understanding Getline() Function in C++

cin is an object used to get input from the user, but it does not allow multi-line input. To accept multi-line input, we use the getline() function. The getline() function is a predefined function defined in the <string.h> header file, used to accept a line or a string from the input stream until a delimiter character is encountered.

Syntax of getline() Function:

There are two ways to declare the function:

  • The first declaration method passes three parameters.

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

The above syntax includes three parameters: is, str, and delim.

Where,

is: an object of the istream class, defines where to read the input stream from.

str: a string object that stores the string.

delim: the delimiter character.

Return Value

The function returns the input stream object passed as a parameter to the function.

  • The second declaration method passes two parameters.

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

The above syntax includes two parameters: is and str. This syntax is almost the same as the previous one, with the only difference being that it does not have a delimiter character.

Where,

is: an object of the istream class, defines where to read the input stream from.

str: a string object that stores the string.

Return Value

This function also returns the input stream object passed as a parameter to the function.

👇 Click to receive 👇
👉 C Language Knowledge Resource Collection

Let’s understand with an example.

First, we will look at an example where we do not use the getline() function to get user input.

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

In the above code, we use cin >> name statement to get user input, which means we did not use the getline() function.

Output

Enter your name : John Miller
Hello John

In the above output, we input the name “John Miller” as user input, but only “John” is displayed. Therefore, we conclude that cin does not consider the characters after a space character.

Let’s solve the above problem by using the getline() function.

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

In the above code, we use the getline() function to accept characters, which can accept even when encountering space characters.

Output

Enter your name : John Miller
Hello John Miller

In the above output, we can observe the two words “John” and “Miller,” which means the getline() function also considers the characters after space characters.

When we do not want to read the characters after space, we use the following code:

#include <iostream>
#include <string.h>
using namespace std;
int main() {
  string profile; // Variable declaration
  std::cout << "Enter your profile :" << std::endl;
  getline(cin, profile, ' '); // Using getline() function with delimiter character.
  cout << "\nProfile is :" << profile;
}

In the above code, we use the getline() function to accept characters, but this time we also add a delimiter character (‘ ‘) in the third parameter. The delimiter character here is a space character, which means characters appearing after the space character will not be considered.

Output

Enter your profile : Software Developer
Profile is: Software

Getting getline() character array We can also define the getline() function for character arrays, but its syntax is different from the previous syntax.

Syntax

istream& getline(char*, int size);

In the above syntax, there are two parameters, one is char*, and the other is size.

Where,

char*: It is a character pointer pointing to an array.

size: It acts as a separator, defining the size of the array, meaning the input cannot exceed this size.

Let’s understand with an example.

#include <iostream>
#include <string.h>
using namespace std;
int main() {
  char fruits[50]; // Array declaration
  cout << "Enter your favorite fruit: ";
  cin.getline(fruits, 50); // Using getline() function
  std::cout << "\nYour favorite fruit is :" << fruits << std::endl;
  return 0;
}

Output

Enter your favorite fruit: Watermelon
Your favorite fruit is: Watermelon

Understanding Getline() Function in C++

Popular Recommendations
  • CLion Tutorial – External Files in CLion

  • C Language Algorithm – “Interleaving String” Algorithm Problem

  • C++ Tutorial – Detailed Explanation of Files and Streams in C++

Leave a Comment