cin getline
时间: 2023-08-07 15:19:18 浏览: 94
详解C++ cin.getline函数
"Cin" and "getline" are both input functions in C++. "Cin" is used to read input from the standard input stream (usually the keyboard), while "getline" is used to read a line of text from any input stream (such as a file or network connection) and store it as a string.
The syntax for using "cin" is as follows:
```c++
#include <iostream>
int main() {
int num;
std::cout << "Enter a number: ";
std::cin >> num;
std::cout << "You entered: " << num << std::endl;
return 0;
}
```
The syntax for using "getline" is as follows:
```c++
#include <iostream>
#include <string>
int main() {
std::string input;
std::cout << "Enter some text: ";
std::getline(std::cin, input);
std::cout << "You entered: " << input << std::endl;
return 0;
}
```
In the first example, the user is prompted to enter a number, which is then stored in the variable "num" using "cin". In the second example, the user is prompted to enter some text, which is then stored in the string variable "input" using "getline".
阅读全文