cin.getline
时间: 2023-10-16 12:30:06 浏览: 51
基于C++ cin、cin.get()、cin.getline()、getline()、gets()函数的使用详解
5星 · 资源好评率100%
() 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.
阅读全文