通过示例了解动态结构数组的运用:
#include<iostream>
struct car
{
char mfrs[100];
int made_year;
};
int main()
{
using namespace std;
int size;
cout << "How many cars do you wish to catalog?";
cin >> size;
car *ps = new car[size];
for(int i = 0; i < size; i++)
{
cout << "Car #" << i + 1 << endl;
cout << "Please enter the make: ";
cin.get();
cin.getline(ps[i].mfrs,100);
cout << "Please enter the year made: " ;
cin >> (ps[i]). made_year;
}
cout << "Here is your collection: " << endl;
for(int i = 0; i < size; i++)
{
cout << ps[i].made_year << " " << ps[i].mfrs << endl;
}
delete ps;
return 0;
}