C++ is a widely popular general-purpose programming language that Bjarne Stroustrup developed. It is built upon the C programming language and was initially named “C with Classes.” The programming language follows object-oriented programming fundamentals and is the fourth most popular programming language worldwide. This article will teach you about C++ getline, which is a fundamental concept when it comes to programming.
What is C++ Getline?
The C++ getline() is an in-built function defined in the <string.h> header file that allows accepting and reading single and multiple line strings from the input stream. In C++, the cin object also allows input from the user, but not multi-word or multi-line input. That’s where the getline() function comes in handy.
The function continues accepting inputs and appending them to the string until it encounters a delimiting character. Thus, you can use it to keep adding inputs for longer strings. Some primary applications include:
- Taking full name
- Taking details such as address and bio
- Asking for any long-form or multi-line input
What is the Syntax of Getline in C++?
There are two different ways of declaring and initializing the C++ getline: three parameters and two parameters. The syntax for declaring the function with three parameters is:
istream& getline (istream& is, string& str, char delimiting);
In the above syntax, istream& getline is to define the function, and the three parameters are:
- istream& is: This is the istream class’s object to define the location, to read the input stream.
- istream& str: This is the object where the string is stored after reading.
- char delimiting: This is the delimiting character that marks the end of taking inputs.
The second method of declaring the C++ getline() function with two parameters is:
istream& getline( istream& is, string& str );
In the above syntax, istream& getline is to define the function, and the three parameters are:
- istream& is: This is an istream class’s object to specify the location to read the input stream.
- istream& str: This is the object where the string is stored after reading.
Return Value
Regardless of the syntax, the getline() function returns the input stream that you pass as an argument to the function.
C++ Getline Examples to Understand How it Works
To understand the C++ getline() function, you will take the user’s name as the input and display a greeting along with the name. You will start by using the cin object and not the getline() function, for better understanding. You will later use the function to see the difference between the two.
Example: Using cin Object
#include <iostream>
#include<string.h>
using namespace std;
int main(){
string user_name; // declaring variable
// taking user input with cin
std::cout << "What is your name? :" << std::endl;
cin>>user_name;
// printing the greeting
cout<<"\nWelcome to Simplilearn "<<user_name;
return 0;
}
Output:
As you can see in the output, only the first name of the user was displayed. That’s because the compiler stopped reading the input stream as soon as it encountered a space (“ ”) character. Thus, you can conclude that the cin object cannot read multi-word or multi-line input. That’s why you need to use the getline function.
Example: Using C++ Getline with Two Parameters
Let’s use the C++ getline() function to greet the user from the above example using his complete name. For this example, you will use the getline() function with two parameters. This means you will not be passing the delimiting character. Let’s see it in action.
#include <iostream>
#include<string.h>
using namespace std;
int main(){
string user_name; // declaring variable
// taking user input with cin
std::cout << "What is your name? :" << std::endl;
// using getline
getline(cin, user_name);
cout<<"\nWelcome to Simplilearn "<<user_name;
return 0;
}
Output:
This time you get both David and Morrison in the output. It means that the C++ getline() function can continue reading the input stream even after encountering a space character.
Example: Using C++ Getline With Three Parameters
You can also use a delimiting character to stop the getline() function from reading the input stream after it encounters that character. To see that in action, you will use the space (“ ”) as a delimiting character to prevent the function from reading the last name after space. Thus, it will give the same output as the one where you used the cin object.
#include <iostream>
#include<string.h>
using namespace std;
int main(){
string user_name; // declaring variable
// taking user input with cin
std::cout << "What is your name? :" << std::endl;
// using getline
getline(cin, user_name, ' ');
cout<<"\nWelcome to Simplilearn "<<user_name;
return 0;
}
Output:
As you can see, although you are using the getline() function, the output displayed only the first name as used space is a delimiting character.
How to Use C++ Getline for Character Array
You can also use C++ getline() function for a character array. However, the syntax differs from what you have seen for the strings. The syntax to use getline character array is:
istream& getline(char* , int size);
In the above syntax:
- char: This is the character pointer that points to the array.
- int size: This defines the maximum size of the array. Thus, it acts as the delimiter, since exceeding the limit will stop the reading.
Example: Getline Character Array Use
#include <iostream>
#include<string.h>
using namespace std;
int main(){
char cars[50]; // array declaration
cout<< "Enter the name of some cars: ";
// using getline function
cin.getline(cars, 50);
std::cout << "\nCars list :"<<cars << std::endl;
return 0;
}
Output:
Example: C++ Getline for Character Array and Exceeding Char Limit
In this example, you will intentionally exceed the char limit to see how the C++ getline() function reacts and stops reading the input stream.
#include <iostream>
#include<string.h>
using namespace std;
int main(){
char cars[50]; // array declaration
cout<< "Enter the name of some cars: ";
// using getline function
cin.getline(cars, 50);
std::cout << "\nCars list :"<<cars << std::endl;
return 0;
}
Output:
As you can see in the output, the getline() function stopped reading the input stream as soon as the character limit, set to 30, was reached.
Choose The Right Software Development Program
This table compares various courses offered by Simplilearn, based on several key features and details. The table provides an overview of the courses' duration, skills you will learn, additional benefits, among other important factors, to help learners make an informed decision about which course best suits their needs.
Program Name Automation Testing Masters Program Full Stack Developer - MEAN Stack Caltech Coding Bootcamp Geo All All US University Simplilearn Simplilearn Caltech Course Duration 11 Months 11 Months 6 Months Coding Experience Required Basic Knowledge Basic Knowledge Basic Knowledge Skills You Will Learn Java, AWS, API Testing, TDD, etc. HTML, CSS, Express.js, API Testing, etc. Java, JavaScript, Angular, MongoDB, etc. Additional Benefits Structured Guidance
Learn From Experts
Hands-on TrainingBlended Learning Program
Learn 20+ Tools and Skills
Industry Aligned ProjectsCaltech Campus Connect
Career Services
17 CEU CreditsCost $$ $$ $$$$ Explore Program Explore Program Explore Program
Conclusion
In this article, you learned about the C++ getline() function. You have also seen its use for character array, along with examples. You can now use it to get long-form multi-line inputs from the users quickly. Knowing about and using the C++ getline is a fundamental concept in this programming language. If you are keen to learn more about such basic concepts, you can refer to Simplilearn’s C++ Tutorial for Beginners. The tutorial covers essential concepts like C++ array and C++ for loop. With the basics clear, you can proceed with the advanced concepts to excel in C++ development. Also, learn how to write the Hello World program in C++ in our next tutorial.
If you are interested in C++, you will surely benefit from comprehensive learning of today’s top programming languages. Simplilearn’s Post Graduate Program in Full Stack Web Development is definitely something that should suit your needs. A coding boot camp in collaboration with Caltech CTME. this 9-month globally-recognized applied to learn program offers you a chance to gain world-ready proficiency in over 30 in-demand full-stack tools and skills with live online classes from global experts, masterclasses from the faculty at Caltech CTME, and a host of industry-aligned projects to practice and perfect your skills. Do check out the course and get started today.
Have any questions for us? Leave them in the comments section of this article. Our experts will get back to you as soon as possible!