C++ Primer Plus Chapter 5 Exercise 6

c plus plusExercise 6 ask us to create a struct. We have already made structs that are very similar to this (see ch4 exercise 9). We employ the new keyword and create a point to a dynamic array. This way, we recycle the same struct, but change the data within it for different cars. See my solution below:

6. Design a structure called car that holds the following information about an automobile: its make, as a string in a character array or in a string object, and the year it was built, as an integer. Write a program that asks the user how many cars to catalog. The program should then use new to create a dynamic array of that many car structures. Next, it should prompt the user to input the make (which might consist of more than one word) and year information for each structure. Note that this requires some care because it alternates reading strings with numeric data (see Chapter 4). Finally, it should display the contents of each structure. A sample run should look something like the following:

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

#include <iostream>
#include <string>

using namespace std;

// create car struct
struct car
{
string make;
int yearBuilt;
};

int main()
{
int cars;

cout << "How many cars do you wish to catalog? ";
cin >> cars;
cout << "\n";

// create dynmaic array with new
car * dynamicArray = new car[cars];

// iterate through our dynamic array
for(int i = 0; i < cars; i++)
{
cout << "For car #" << i+1 << endl;
cout << "Please enter the make: ";
cin >> dynamicArray[i].make;
cout << "Please enter the year made: ";
cin >> dynamicArray[i].yearBuilt;
}

// output our collection
cout << "Here is what is in your collection:" << endl;
for(int i = 0; i < cars; i++)
cout << dynamicArray[i].yearBuilt << " " << dynamicArray[i].make << endl;

// clean
delete [] dynamicArray;

return 0;
}