Cin

     
In this article, we"ll talk about the getline() function in C++. This is an inbuilt function that accepts single và multiple character inputs.

Bạn đang xem: Cin

When working with user input in C++, the cin object allows us khổng lồ get input đầu vào information from the user. But when we try lớn log out the user"s đầu vào that has multiple values, it only returns the first character.

This happens because the C++ compiler assumes that any trắng space terminates the program when getting the input. That is, "My name is Ihechikara" would only return "My" when logged out.

Here is a better example:

#include using namespace std;int main() { string bio; // Information logged to lớn the console cout > bio; /* When logging out the bio inputed above, only "JavaScript" was logged out */ cout In the code above, the user is asked to đầu vào their bio. They went on to input "JavaScript is my favorite language". But when the bio was logged lớn the console, only "JavaScript" was logged out.

Next, we"ll see how lớn use the getline() function khổng lồ get the rest of the characters in the string.

C++ getline() Function Example

In this section, we"ll see a practical example of using the getline() function.

#include using namespace std;int main() { string bio; cout In the example above, we passed in two parameters in the getline() function: getline(cin, bio);. The first parameter is the cin object while the second is the bio string variable.

When you run the code, you"ll be prompted to đầu vào some text. After you"ve done that, hit enter and see the đầu ra that has all the text from your input đầu vào instead of just the first character.

Xem thêm: Các Bài Toán Tính Nhanh Lớp 5 Có Đáp Án, Chuyên Đề Toán Tính Nhanh Lớp 5

In my case, I typed in a string with multiple characters và got it logged out khổng lồ the console. Go on and try it to lớn see how it works.

With this, you can work effectively with user inputs in your programs.

Conclusion

In this article, we talked about the getline() function which enables us get multiple characters from a user"s input.

We first saw what happens when we get a string with multiple characters from a user – only the first character is returned.

We then saw how khổng lồ get all the characters from the string using the getline() function which takes two parameters – the cin object & the string variable.

Happy coding!


*
Ihechikara Vincent Abba

This author's bio can be found in his articles!


giayphutyeuthuong.vn is a donor-supported tax-exempt 501(c)(3) nonprofit organization (United States Federal Tax Identification Number: 82-0779546)

Our mission: lớn help people learn lớn code for free. We accomplish this by creating thousands of videos, articles, & interactive coding lessons - all freely available to the public. We also have thousands of giayphutyeuthuong.vn study groups around the world.

Xem thêm: Ngữ Văn 6 Mây Và Sóng Lớp 6 (Chân Trời Sáng Tạo), Soạn Bài Mây Và Sóng Lớp 6 (Chân Trời Sáng Tạo)

Donations khổng lồ giayphutyeuthuong.vn go toward our education initiatives, và help pay for servers, services, và staff.