C++ Primer Plus Chapter 4 Exercise 9

c plus plusExercise 9 is a remake of exercise 6, a common theme in this chapter. Only a few things need to be swapped in order to meet the requirements.  We initially declare a single structure candyBar, then we create a dynamic array candyBar[3].  *bar points to the first element of candyBar[3], and we can access candyBar by calling bar. Here is my solution:

Do Programming Exercise 6, but, instead of declaring an array of three CandyBar structures,
use new to allocate the array dynamically.

#include <iostream>
#include <string>

using namespace std;

// Candy bar structure
struct candyBar
{
string brand;
double weight;
int calories;
};

int main()
{
// create three members, but use new to allocate
candyBar * bar = new candyBar[3];

bar[0].brand = "Crunch";
bar[0].weight = 1.7;
bar[0].calories = 275;

bar[1].brand = "Heath";
bar[1].weight = 2.3;
bar[1].calories = 400;

bar[2].brand = "Rolo";
bar[2].weight = 2.5;
bar[2].calories = 350;

// Ouput bars
cout << "The first bar variable holds: \n";
cout << bar[0].brand << "\n";
cout << bar[0].weight << " ounces \n";
cout << bar[0].calories << " calories \n";
cout << endl;

cout << "The second bar variable holds: \n";
cout << bar[1].brand << "\n";
cout << bar[1].weight << " ounces \n";
cout << bar[1].calories << " calories \n";
cout << endl;

cout << "The third bar variable holds: \n";
cout << bar[2].brand << "\n";
cout << bar[2].weight << " ounces \n";
cout << bar[2].calories << " calories \n";

delete [] bar; // free memory
cin.get();
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;

cin.get();
return 0;
}

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
information:
• 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;

cin.get();
return 0;
}

C++ Primer Plus Chapter 4 Exercise 6

c plus plusFor exercise six we can copy and paste our previous code and simply make some minor adjustments to the source to complete exercise six. This time we declare our snack variable as an array of three, and initialize the member within braces. I went with my three favorite candy bars fir this one 🙂 I think the goal of this exercise was to show that we can have either one thing with relevant variables or we can create structs with many relevant member search their own variables. Here is my solution:

6. The CandyBar structure contains three members, as described in Programming Exercise
5. Write a program that creates an array of three CandyBar structures, initializes them to
values of your choice, and then displays the contents of each structure.

#include <iostream>
#include <string>

using namespace std;

// structure
struct candyBar
{
string brand;
double weight;
int calories;
};

int main()
{
// create three members to our struct
candyBar bar[3] =
{
{"Crunch",1.7,275},
{"Heath",2.3,400},
{"Rolo",2.5,350}
};

cout << "The first bar variable holds: \n";
cout << bar[0].brand << "\n";
cout << bar[0].weight << " ounces \n";
cout << bar[0].calories << " calories \n";
cout << endl;

cout << "The second bar variable holds: \n";
cout << bar[1].brand << "\n";
cout << bar[1].weight << " ounces \n";
cout << bar[1].calories << " calories \n";
cout << endl;

cout << "The third bar variable holds: \n";
cout << bar[2].brand << "\n";
cout << bar[2].weight << " ounces \n";
cout << bar[2].calories << " calories \n";

cin.get();
return 0;
}

C++ Primer Plus Chapter 4 Exercise 5

c plus plusExercise five requires us to make a basic struct. Structs are useful for when you want to hold relevant information about an object. With structs we are getting closer to OOP programming.  Here is my solution to the problem:

5. The CandyBar structure contains three members. The first member holds the brand
name of a candy bar. The second member holds the weight (which may have a fractional
part) of the candy bar, and the third member holds the number of calories (an integer
value) in the candy bar. Write a program that declares such a structure and creates a
CandyBar variable called snack, initializing its members to “Mocha Munch”, 2.3, and
350, respectively. The initialization should be part of the declaration for snack. Finally,
the program should display the contents of the snack variable.

#include <iostream>
#include <string>

using namespace std;

struct candyBar
{
string brand;
float weight;
int calories;
};

int main()
{
candyBar snack = {"Mocha Munch", 2.3, 350};
cout << "The snack variable holds: \n";
cout << snack.brand << "\n";
cout << snack.weight << " ounces \n";
cout << snack.calories << " calories \n";

cin.get();
return 0;
}