问题:
解答:
#include <iostream>
#include <string>
using namespace std;
typedef struct _Car
{
string brand;
int year;
}Car;
int main()
{
int count = 0;
cout << "请问你家有多少辆车呢?" << endl;
cin >> count;
cin.get();
Car* cars = new Car[count];
for (int i = 0; i < count; i++)
{
cout << "请你简绍一下你的第" << i + 1 << "辆车的生产商" << endl;
getline(cin, cars[i].brand);
cout << "随便说说其生成年份" << endl;
cin >> cars[i].year;
cin.get();
}
cout << "此人旗下拥有的车有如下:" << endl;
for (int i = 0; i < count; i++)
{
cout << "生产商:" << cars[i].brand << endl;
cout << "生产年份:" << cars[i].year << endl << endl;
}
delete[]cars;
return 0;
}
运行结果:
考查点:
- 结构体
- new delete
- 输入缓冲区
- for循环
2024年8月25日20:27:11