Getline trong c++ code example

     

C++ is a widely popular general-purpose programming language that Bjarne Stroustrup developed. It is built upon the C programming language & 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 lớn programming.

Bạn đang xem: Getline trong c++ code example


*

What is C++ Getline?

The C++ getline() is an in-built function defined in the header tệp tin that allows accepting & reading single & multiple line strings from the input stream. In C++, the cin object also allows input đầu vào 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 lớn 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 nameTaking details such as address and bioAsking for any long-form or multi-line input

What is the Syntax of Getline in C++?

There are two different ways of declaring và initializing the C++ getline: three parameters và 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 lớn define the function, và the three parameters are:

istream& is: This is the istream class’s object to define the location, lớn 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 over 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 khổng lồ define the function, and the three parameters are:

istream& is: This is an istream class’s object lớn specify the location to lớn 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 đầu vào stream that you pass as an argument khổng lồ the function.

C++ Getline Examples lớn Understand How it Works

To understand the C++ getline() function, you will take the user’s name as the input và display a greeting along with the name. You will start by using the cin object & not the getline() function, for better understanding. You will later use the function lớn see the difference between the two.

Example: Using cin Object

#include

#include

using namespace std;

int main(){

string user_name; // declaring variable

// taking user đầu vào with cin

std::cout >user_name;

// printing the greeting

coutOutput:

*

As you can see in the output, only the first name of the user was displayed. That’s because the compiler stopped reading the đầu vào 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 lớn use the getline function.


New Course: Full Stack Development for BeginnersLearn Git Command, Angular, NodeJS, Maven và MoreEnroll Now
*

Example: Using C++ Getline with Two Parameters

Let’s use the C++ getline() function lớn 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

#include

using namespace std;

int main(){

string user_name; // declaring variable

// taking user input with cin

std::cout 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.

Xem thêm: Bài Giảng Điện Tử, Giáo Án Điện Tử Môn Khoa Học Lớp 4, Khoa Học Lớp 4

Example: Using C++ Getline With Three Parameters

You can also use a delimiting character lớn stop the getline() function from reading the input stream after it encounters that character. Lớn see that in action, you will use the space (“ ”) as a delimiting character khổng lồ 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

#include

using namespace std;

int main(){

string user_name; // declaring variable

// taking user đầu vào with cin

std::cout Output:

*

As you can see, although you are using the getline() function, the đầu ra displayed only the first name as used space is a delimiting character.


*

How lớn 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 lớn the array.int size: This defines the maximum form size of the array. Thus, it acts as the delimiter, since exceeding the limit will stop the reading.

Example: Getline Character Array Use

#include

#include

using namespace std;

int main(){

char cars<50>; // array declaration

coutOutput:

*

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 và stops reading the input đầu vào stream.

#include

#include

using namespace std;

int main(){

char cars<50>; // array declaration

coutOutput:

*

As you can see in the output, the getline() function stopped reading the input stream as soon as the character limit, set lớn 30, was reached.

Get noticed by top hiring companies through our JobAssist program. Get complete job assistance post the Full Stack website Developer - MEAN Stack Developer Course & unleash the endless possibilities. Get in cảm biến with our admission counselor TODAY!

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 lớn get long-form multi-line inputs from the users quickly. Knowing about & using the C++ getline is a fundamental concept in this programming language. If you are keen to lớn learn more about such basic concepts, you can refer lớn onip.vn’s C++ Tutorial for Beginners. The tutorial covers essential concepts lượt thích C++ array & C++ for loop. With the basics clear, you can proceed with the advanced concepts lớn excel in C++ development. Also, learn how khổng lồ write the Hello World program in C++ in our next tutorial.

Xem thêm: Bình Xịt Tưới Cây 2 Lít Giá Tốt Tháng 4, 2022, Bình Phun Xịt 2 Lít Dudaco, Đầu Vòi Phun Lắp Đồng

If you are interested in C++, you will surely benefit from comprehensive learning of today’s top programming languages. onip.vn’s Post Graduate Program in Full Stack website 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 lớn learn program offers you a chance to lớn gain world-ready proficiency in over 30 in-demand full-stack tools & skills with live online classes from global experts, masterclasses from the faculty at Caltech CTME, và a host of industry-aligned projects to lớn practice & perfect your skills. Do kiểm tra out the course và 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!