What are Getline Function in C++?

What are Getline Function in C++?

6 mins read1.5K Views Comment
Updated on Jun 19, 2023 17:12 IST

In C++, getline() is a standard library function in C++ that is used to read a line of text from an input stream and store it as a string. Getline can also be used to extract characters from an input stream (such as the standard input stream std::cin) until a newline character or the end-of-file is reached.

2023_02_MicrosoftTeams-image-272.jpg

In this tutorial, we will discuss getline in C++ and understand its working through code examples. We will be covering the following sections: 

What is Getline in C++? 

The getline() function in C++ is a built-in tool in the <string.h> library that allows you to receive and read strings, including multi-line strings, from an input stream. Unlike the cin object, which can only accept single-word inputs, getline() allows you to capture longer, multi-word strings. The function continuously reads input and appends it to the string until it reaches a specified delimiting character.  

You can also explore: Erasing Elements from a Vector in C++

The function takes three arguments: the input stream to read from, the string variable to store the input, and the character that is used to delimit the end of the line (the default delimiter is the newline character). And, once the function takes three arguments, it returns the input stream, allowing for chaining of input operations.  

You can also explore: C++ Structure and Functions

Explore: Free C++ Courses Online

Check: C++ Online Courses & Certifications

Syntax 

The syntax to declare getline in C++ is shown below: 

 
std::getline(input_stream, string_variable, delimiter);
Copy code

Where: 

  • input_stream is the input stream to read from (e.g. std::cin). 
  • string_variable is the string variable to store the input. 
  • delimiter is the character used to delimit the end of the line (the default delimiter is the newline character). 
Recommended online courses

Best-suited C++ courses for you

Learn C++ with these high-rated online courses

– / –
4 months
β‚Ή4.24 K
6 weeks
– / –
15 days
– / –
– / –
– / –
70 hours
– / –
3 months
– / –
1 hours
Free
2 hours
Free
1 hours
Free
1 hours

How Getline Works in C++? 

This function in C++ works by extracting characters from an input stream (such as std::cin) and storing them in a string until a specified delimiter is encountered. 

Let’s go through an example in order to understand how you read a full name using this function in C++: 

 
#include <iostream>
#include <string>
int main() {
std::string name;
std::cout << "Enter your name: ";
std::getline(std::cin, name);
std::cout << "Your name is: " << name << std::endl;
return 0;
}
Copy code

Output: 

2023_02_image-4.jpg

Explanation: 

In this example, std::getline is called with std::cin as the input stream, name as the string variable to store the input, and the newline character as the delimiter. The function reads characters from std::cin and stores them in name until the newline character is encountered. Finally, the program outputs the contents of name to the screen. 

You can also explore: Ceil function in C++

Examples of Using Getline in C++ 

Example 1: Reading a Sentence in C++ 

 
#include <iostream>
#include <string>
int main() {
std::string sentence;
std::cout << "Enter a sentence: ";
std::getline(std::cin, sentence);
std::cout << "Your sentence is: " << sentence << std::endl;
return 0;
}
Copy code

Output: 

2023_02_image-4.jpg

In this program, getline is used to read a sentence with multiple words from the input stream and store it in a string variable sentence. The user can enter a sentence of any length, including spaces, and the function will store the entire sentence in sentence

You can also explore: Member function in C++

Example 2: Read a multi-line paragraph  

 
#include <iostream>
#include <string>
int main() {
std::string paragraph;
std::cout << "Enter a paragraph: " << std::endl;
std::getline(std::cin, paragraph, '\0');
std::cout << "Your paragraph is: " << std::endl << paragraph << std::endl;
return 0;
}
Copy code

In this program, getline is used to read a multi-line paragraph from the input stream and store it in a string variable paragraph. The delimiter character is set to β€˜\0’, which represents the end-of-file marker, to indicate that the input should continue until the end-of-file is reached.  

You can also explore: ROUND() Function in C++

How to Use Getline For Character Array? 

In C++, you can use this function with a character array as well. The syntax for using this function with a character array is different from what you have seen for strings. 

The syntax to use getline character array is shown below: 

 
cin.getline(array_name, size);
Copy code

Where array_name is the name of the character array and size is the maximum number of characters, including the null terminator, to read from the input stream. 

You can also explore: All About Virtual Functions in C++

Let’s go through an example in order to understand how you can use getline to read input into a character array in C++” 

 
#include <iostream>
#include <cstring>
int main() {
char sentence[100];
std::cout << "Enter a sentence: ";
std::cin.getline(sentence, 100);
std::cout << "Your sentence is: " << sentence << std::endl;
return 0;
}
Copy code

Output: 

2023_02_image-5.jpg

In this example, cin.getline is used to read input into a character array sentence of size 100. The second argument of this function specifies the maximum number of characters to read, including the null terminator. 

You can also explore: All About C++ Friend Function

Note that when using this function with a character array, you must use cin.getline instead of std::getline. This is because cin is an object of type istream, which is the input stream class, while sentence is a character array, which is a type of char*

Endnotes 

Whether you are working with strings or character arrays, getline provides a flexible and efficient way to handle multi-line inputs, and is widely used in various programming applications. Understanding how to use this function is an important part of developing effective C++ programs, and is a valuable tool for any programmer. 

Hope this article helped you grasp the concept of getline in C++. Explore our C++ articles to find out more about the language and consolidate your knowledge of the fundamentals. β€― 

FAQs

What is the getline function in C++?

The getline function is a C++ standard library function that is used to read input from a stream until a delimiter character is found. The function is commonly used for reading strings of text input.

What header file is required for using the getline function?

The getline function is defined in the header file, so this header file is required for using the getline function in C++.

How do I use the getline function to read input from the user?

To use the getline function to read input from the user, you can call the function and pass in the input stream as the first argument, and the string variable to store the input as the second argument.

What is the difference between cin and getline for input?

cin is used to read input until whitespace is encountered, whereas getline is used to read input until a specified delimiter character is encountered. This means that getline can be used to read a whole line of text input, whereas cin only reads up to the first whitespace character.

How do I use the getline function to read input from a file?

To use the getline function to read input from a file, you can call the function and pass in the input file stream as the first argument, and the string variable to store the input as the second argument.

Can I specify a delimiter for the getline function to use?

Yes, you can specify a delimiter character for the getline function to use. By default, the delimiter is the newline character, but you can specify a different delimiter by passing it as the third argument to the function.

How does the getline function handle leading and trailing whitespace?

The getline function removes any leading whitespace characters from the input string, but it leaves any trailing whitespace characters intact. This means that if there is whitespace at the end of the line, it will be included in the input string.

Can I use the getline function to read input from standard input and a file?

Yes, you can use the getline function to read input from both standard input and a file. To read from standard inputTo read from a file, open the file using an input file stream and pass it as the input stream argument.

What happens if the getline function reaches the end of the file?

If the getline function reaches the end of the file before finding the delimiter character, it sets the end-of-file flag on the input stream and returns the input string without the delimiter.

How can I handle errors that occur when using the getline function?

You can check the state of the input stream after calling getline to handle errors that occur. If the input stream is in a fail or bad state, an error has occurred. You can use the clear() function to clear any error flags and continue reading input.

About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio