cin.getline
时间: 2023-09-24 08:05:37 浏览: 63
基于C++ cin、cin.get()、cin.getline()、getline()、gets()函数的使用详解
5星 · 资源好评率100%
() is a function in C++ that reads a line of input from the user and stores it in a character array. It takes two arguments: the first is the name of the character array where the input will be stored, and the second is the maximum number of characters to read.
Here's an example of how to use cin.getline():
```
#include <iostream>
using namespace std;
int main() {
char name[20];
cout << "Enter your name: ";
cin.getline(name, 20);
cout << "Hello, " << name << endl;
return 0;
}
```
In this example, we declare a character array called name with a maximum size of 20 characters. We then prompt the user to enter their name using cout, and read their input using cin.getline(). Finally, we print out a personalized greeting using their name.
Note that cin.getline() will stop reading input when it encounters a newline character (\n) or when it has read the maximum number of characters specified. Any remaining characters on the input stream will be left there for the next input operation to read.
阅读全文