题目:
7.设计一个名为car的结构,用它存储下述有关汽车的信息:生产商(存储在字符数组或string对象中的字符串)、生产年份(整数)。编写一个程序,向用户询问有多少辆汽车。随后,程序使用new来创建一个由相应数量的car结构组成的动态数组。接下来,程序提示用户输入每辆车的生产商(可能由多个单词组成)和年份信息。请注意,这需要特别小心,因为它将交替读取数值和字符串(参见第4章)。最后,程序将显示每个结构的内容。该程序的运行情况如下:
How many cars do you wish to catalog? 2
Car #1:
Please enter the make: Hudson Hornet
Please enter the year made: 1952
Car #2:
Please enter the make: Kaiser
Please enter the year made: 1951
Here is your collection:
1952 Hudson Hornet
1951 Kaiser
答案: 书上无答案。
C++技术网辅导详解解答:
参考代码:
#include <iostream>
using namespace std;
struct car {
char oem[200];
int year;
};
int main()
{
cout << "How many cars do you wish to catalog :";
int num;
cin >> num;
cin.get();//除掉多余的回车符号
struct car *pData = new struct car[num];
for (int i = 0; i < num; i++)
{
cout << "Car #" << i + 1 << ":\n";
cout << "Please enter the make :";
cin.getline(pData[i].oem,200);
cout << "Please enter the year made :";
cin >> pData[i].year;
cin.get();//除掉多余的回车符号
}
cout << "Here is your collection :\n";
for (int i = 0; i < num; i++)
{
cout << pData[i].year << " " << pData[i].oem << endl;
}
delete[] pData;
return 0;
}