C++ Primer Plus Chapter 4 Exercise 7

c plus plusExercise 7 is fairly straightforward. We are constructing another data structure modeled after a pizza. The only difference here is that we are making the structs member variables instead of hard coding them. Here is my solution:

William Wingate runs a pizza-analysis service. For each pizza, he needs to record the following
• The name of the pizza company, which can consist of more than one word
• The diameter of the pizza
• The weight of the pizza
Devise a structure that can hold this information and write a program that uses a structure
variable of that type. The program should ask the user to enter each of the preceding
items of information, and then the program should display that information. Use cin
(or its methods) and cout.

#include <iostream>
#include <string>

using namespace std;

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

int main()

pizza pie = {pie.pizzaCompanyName, pie.pizzaDiameter, pie.pizzaWeight};

// Gather info
cout << "Enter the name of the Pizza Company: ";
getline(cin, pie.pizzaCompanyName);
cout << "Enter the diameter of the pizza in inches: ";
cin >> pie.pizzaDiameter;
cout << "Enter the weight of the pizza in ounces: ";
cin >> pie.pizzaWeight;
cout << "\n\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;

return 0;


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s