引用:
踏魔狼 在 2008-7-21 01:50 的发言:
在cin.getline(pc->name, 20);
之前加上
cin.clear();
cin.ignore(1024, '\n');
#include<iostream>
#include<string>
using namespace std;
struct car
{char name[20];
int year;
};
int main()
{int i;
cout<<"How many cars do you wish to catalog? ";
cin>>i;
car * pc=new car[i];
cout<<"Car #1: "<<endl;
cout<<"Please enter the make: ";
cin.clear();
cin.ignore(1024, '\n');
cin.getline(pc->name,20);
cout<<"please enter the year made: ";
cin>>pc->year;
cout<<"Car #2: "<<endl;
cout<<"please enter the make: ";
cin.clear();
cin.ignore(1024, '\n');
cin.getline((pc+1)->name,20);
cout<<"please enter the year made: ";
cin>>(pc+1)->year;
cout<<"Here is your collection; "<<endl;
cin.ignore();
cout<<pc->year<<" "<<pc->name<<endl;
cout<<(pc+1)->year<<" "<<(pc+1)->name<<endl;
delete [] pc;
return 0;
}
非常感谢你的回答,由于我C++学习的还是很前面,请问你能帮我解释下为什么要在1之前加上2和3吗?由其是cin.ignore(1024, '\n')中的1024参数不懂~
cin.clear();//2
cin.ignore(1024, '\n');//3
cin.getline(pc->name,20);//1