Share getline c++

happymeercat245

New member
## Getline C ++

** getline () trong c ++ là gì? **

Hàm `getline ()` trong C ++ được sử dụng để đọc một dòng văn bản từ luồng đầu vào tiêu chuẩn (stdin).Nó là một hàm thành viên của lớp `istream` và nó có hai đối số: một tham chiếu đến một đối tượng` istream` và một con trỏ tới một mảng ký tự.

Hàm `getline ()` đọc các ký tự từ luồng đầu vào cho đến khi nó đạt đến ký tự dòng mới (`\ n`).Sau đó, nó lưu trữ các ký tự mà nó đã đọc vào mảng ký tự được truyền như là đối số thứ hai.Hàm `getline ()` trả về số lượng ký tự mà nó đã đọc, không bao gồm ký tự mới.

** Cách sử dụng getline () trong c ++? **

Mã sau đây cho thấy cách sử dụng hàm `getline ()` để đọc một dòng văn bản từ luồng đầu vào tiêu chuẩn:

`` `C ++
#include <Istream>

int main () {
// Tạo một chuỗi để lưu trữ dòng đầu vào.
STD :: Dòng chuỗi;

// Đọc một dòng văn bản từ luồng đầu vào tiêu chuẩn.
std :: getline (std :: cin, line);

// In dòng đầu vào vào bảng điều khiển.
std :: cout << line << std :: endl;

trả lại 0;
}
`` `

** Ví dụ về getline () trong c ++ **

Mã sau đây hiển thị một ví dụ về việc sử dụng hàm `getline ()` để đọc một dòng văn bản từ luồng đầu vào tiêu chuẩn và sau đó in nó vào bảng điều khiển:

`` `C ++
#include <Istream>

int main () {
// Nhận một dòng đầu vào từ người dùng.
STD :: Dòng chuỗi;
std :: cout << "Nhập một dòng văn bản:";
std :: getline (std :: cin, line);

// In dòng đầu vào vào bảng điều khiển.
std :: cout << "bạn đã nhập:" << line << std :: endl;

trả lại 0;
}
`` `

** Hashtags: **

* C ++
* Đầu vào
* Đầu ra
* Sợi dây
* Dòng

## Người giới thiệu

* [C ++ getLine () hàm] (https://www.cplusplus.com/reference/istream/getline/)
=======================================
## Getline C++

**What is getline() in C++?**

The `getline()` function in C++ is used to read a line of text from the standard input stream (stdin). It is a member function of the `istream` class, and it takes two arguments: a reference to an `istream` object and a pointer to a character array.

The `getline()` function reads characters from the input stream until it reaches a newline character (`\n`). It then stores the characters that it has read into the character array that was passed as the second argument. The `getline()` function returns the number of characters that it has read, not including the newline character.

**How to use getline() in C++?**

The following code shows how to use the `getline()` function to read a line of text from the standard input stream:

```c++
#include <iostream>

int main() {
// Create a string to store the input line.
std::string line;

// Read a line of text from the standard input stream.
std::getline(std::cin, line);

// Print the input line to the console.
std::cout << line << std::endl;

return 0;
}
```

**Example of getline() in C++**

The following code shows an example of using the `getline()` function to read a line of text from the standard input stream and then print it to the console:

```c++
#include <iostream>

int main() {
// Get a line of input from the user.
std::string line;
std::cout << "Enter a line of text: ";
std::getline(std::cin, line);

// Print the input line to the console.
std::cout << "You entered: " << line << std::endl;

return 0;
}
```

**Hashtags:**

* C++
* Input
* Output
* String
* Streams

## References

* [C++ getline() function](https://www.cplusplus.com/reference/istream/getline/)
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top