cin.getline在输入char时:
using namespace std; const int ArSize = 20; char name[ArSize]; char dessert[ArSize]; cout << "Enter your name: "; cin.getline(name, ArSize); // reads through newline cout << "Enter your favorite dessert: "; cin.getline(dessert, ArSize);
getline在输入string时:
#include<iostream> #include<string> using namespace std; int main() { string name; string dessert; cout<<"Enter your name: "; getline(cin,name); cout<<"Enter your favorite dessert: "; getline(cin,dessert);