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;

C++ Primer Plus Chapter 4 Exercise 8

c plus plus

Exercise 8 took a little finagling to make it ask for diameter first while outputting it second in order as the last program, without skipping over the name input. If you use some other methods for pointing to structs at the diameter input, you will see what I mean. I provided a few different methods of pointing to structs in this exercise. The “new” keyword was used as per directions to allocate memory for our structure. Alas, here is my solution:

Do Programming Exercise 7, but use new to allocate a structure instead of declaring a
structure variable. Also, have the program request the pizza diameter before it requests
the pizza company name.

#include <iostream>
#include <string>

using namespace std;

// pizza struct
struct pizza
string pizzaCompanyName;
int pizzaDiameter;
int pizzaWeight;

int main()

// "New" keyword used to allocate memory for the structure
pizza * pie = new pizza;

// Gather info
cout << "Enter the diameter of the pizza in inches: ";
(cin >> pie->pizzaDiameter).get(); // manipulate cin
cout << "Enter the name of the Pizza Company: ";
getline(cin, pie->pizzaCompanyName);
cout << "Enter the weight of the pizza in ounces: ";
cin >> (*pie).pizzaWeight; // Another method of pointing to a structure
cout << "\n";

// Output info
cout << "The pizza company name is: " << pie->pizzaCompanyName << endl;
cout << "The Diameter inches is: " << pie->pizzaDiameter << endl;
cout << "The weight in ounces is: " << pie->pizzaWeight << endl;

// Free memory used by our structure, important.
delete pie;

return 0;