cin.getline
时间: 2023-10-16 21:30:06 浏览: 31
() function
The cin.getline() function is used in C++ to read a line of text from the input stream and store it in a character array. It is similar to the cin >> operator, but can handle input containing spaces and newline characters. The function takes two arguments: the name of the character array where the input will be stored, and the maximum number of characters to read. Here is an example of how to use cin.getline():
```
#include <iostream>
using namespace std;
int main() {
char name[50];
cout << "Enter your name: ";
cin.getline(name, 50);
cout << "Hello, " << name << "!" << endl;
return 0;
}
```
In this example, the user is prompted to enter their name. The cin.getline() function is used to read the input and store it in the character array "name". The maximum number of characters to read is set to 50, to ensure that the input does not exceed the size of the array. The input is then displayed using cout.
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)