主题:关于cin.get()
#include<iostream>
#include<string>
using namespace std;
struct car
{
string name;
int year;
};
int main()
{
int x,i;
cout<<"how many cars do you have?";
cin>>x;
[color=008080]cin.get();[/color]
car* ps=new car[x];
for(i=0;i<x;++i)
{
cout<<"car #"<<i+1<<":\n";
cout<<"please enter the make:";
getline(cin,ps[i].name);
cout<<"please enter the year made:";
cin>>ps[i].year;
}
cout<<"here is your collection:\n";
for(i=0;i<x;++i)
cout<<ps[i].year<<"\t"<<ps[i].name<<endl;
delete [] ps;
return 0;
}
为什么去掉绿色那行cin.get(),程序只接受第一行“please enter the make”输入?
而改用char数组代替string类输入就没有这个问题???
谢谢
#include<string>
using namespace std;
struct car
{
string name;
int year;
};
int main()
{
int x,i;
cout<<"how many cars do you have?";
cin>>x;
[color=008080]cin.get();[/color]
car* ps=new car[x];
for(i=0;i<x;++i)
{
cout<<"car #"<<i+1<<":\n";
cout<<"please enter the make:";
getline(cin,ps[i].name);
cout<<"please enter the year made:";
cin>>ps[i].year;
}
cout<<"here is your collection:\n";
for(i=0;i<x;++i)
cout<<ps[i].year<<"\t"<<ps[i].name<<endl;
delete [] ps;
return 0;
}
为什么去掉绿色那行cin.get(),程序只接受第一行“please enter the make”输入?
而改用char数组代替string类输入就没有这个问题???
谢谢