#include <iostream>
#include <string> // Header file needed to use string objects
using namespace std;
int main()
{
string name;
string city;
cout << "Please enter your name: ";
//cin >> name;//这里输入aa bb只会识别aa空格为结束
getline(cin,name);//可以识别到空格
cout << "Enter the city you live in: ";
cin >> city;
cout << "Hello, " << name << endl;
cout << "You live in " << city << endl;
return 0;
}
getline 是从键盘输入一个以ENTER键标志结束的字符串
与 getline 一样,cin.getline 允许读取包含空格的字符串
cin.getline(a, 6);// 此处输入12345(enter)
cout << a << endl;
最多可以读取5个字符,少一个