C++ Primer Plus Chapter 7 Exercise 3

c plus plusAfter a bit of a break, I am ready to continue these exercises as well as put out some other things I have been working on.  Chapter 7 exercise 3 wants us to create two functions using a structs given values, then use those functions and the struct in a program. Here is my solution to this problem:

3. Here is a structure declaration:
struct box
{
char maker[40];
float height;
float width;
float length;
float volume;
};
a. Write a function that passes a box structure by value and that displays the value of
each member.
b. Write a function that passes the address of a box structure and that sets the volume
member to the product of the other three dimensions.
c. Write a simple program that uses these two functions.

 #include <iostream>

using namespace std;

struct box
{
char maker[40];
float height;
float width;
float length;
float volume;
};

void showValue(box);
void setVolume(box*);

int main()
{
box toyBox = {"Elf", 2.5, 3.3, 5.0, 0};
showValue(toyBox);
setVolume(&toyBox);
cout << "\n\nWith volume calculated: \n";
showValue(toyBox);

return 0;
}

void showValue(box ourStruct)
{
cout << "Struct details\n";
cout << "Struct maker: " << ourStruct.maker << endl;
cout << "Struct height: " << ourStruct.height << endl;
cout << "Struct width: " << ourStruct.width << endl;
cout << "Struct length: " << ourStruct.length << endl;
cout << "Struct volume: " << ourStruct.volume << endl;
}

void setVolume(box* ourStruct)
{
ourStruct->volume = ourStruct->height * ourStruct->width * ourStruct->length;
}
 
Advertisements

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