结构Car包括三个成员,形式如下
struct Car{
char name[10];
int length;
int height;
} Car; 编写程序,创建一个包含三个元素的Car数组,并将其初始化为所选择的值。然后显示每个结构的内容。用new实现。
#include<iostream>
using namespace std;
struct Car {
char name[10];
int length;
int height;
}Car;
int main()
{
int n,i;
struct Car *p;
cout << "how many cars?" << endl;
cin >> n;
p=new struct Car[n];
for(i=0;i<n;i++)
{
cout << "car's name:" << endl;
cin >> p[i].name ;
cout << "car's length:" << endl;
cin >> p[i].length;
cout << "car's height:" << endl;
cin >> p[i].height;
}
cout << "name\tlength\theight\t";
cout << endl;
for(i=0;i<n;i++)
{
cout << p[i].name << "\t";
cout << p[i].length << "\t";
cout << p[i].height << "\t";
cout << endl;
}
delete [] p;
return 0;
}
运行结果