c++中字符輸入函數cin.getline在輸入char與string時的不同
阿新 • • 發佈:2019-01-16
style \n span pre des ace pac using reads
cin.getline在輸入char時:
using namespace std; const int ArSize = 20; char name[ArSize]; char dessert[ArSize]; cout << "Enter your name:\n"; cin.getline(name, ArSize); // reads through newline cout << "Enter your favorite dessert:\n"; cin.getline(dessert, ArSize);
getline在輸入string時:
#include<iostream> #include<string> using namespace std; int main() { string name; string dessert; cout<<"Enter your name:\n"; getline(cin,name); cout<<"Enter your favorite dessert:\n"; getline(cin,dessert);
c++中字符輸入函數cin.getline在輸入char與string時的不同