#include<iostream>
#include<stdlib.h>
using namespace std;
struct car
{
char name[20];
int year;
};
int main()
{
int counts;
int i;
cout << "How many cars do you wish to catalog? ";
cin >> counts;
car *ps = new car [counts];
for (i = 0; i < counts; i++)
{
cout << "Car #" << i + 1 << " :" << endl;
cout << "Please enter the make: ";
cin.get();
cin.getline(ps[i].name, 20);
cout << "Please enter the year made: ";
cin >> ps[i].year;
cin.get();
}
cout << "Here is your collection: " << endl;
for (i = 0; i < counts; i++)
{
cout << ps[i].year << " " << ps[i].name << endl;
}
delete[] ps;
system("pause");
return 0;
}
注意cin.get(),一开始程序没有错误,但是不能输入,加了几个cin.get()就可以了。