Question: C++ Code In Labs 1-3 Makeup, you were required to write 6 while loops to check input for dimensions of 3-dimensional objects. For this lab,

C++ Code

In Labs 1-3 Makeup, you were required to write 6 while loops to check input for dimensions of 3-dimensional objects. For this lab, you should redo Labs 1-3 makeup with function calls replacing the while loops to check dimensions and write a function to check the validity. (I have posted a possible solution for Labs 1-3 makeup that you can use if you did not do Labs 1-3 makeup. Review this possible solution, fix any errors, then make the changes for this lab.)

The new function that you add to check the validity should accept two parameters: one a double and one a string. You should send the dimension that is being checked to the double parameter and a description of what that dimension stands for (e.g. radius of sphere, height of box, etc.) to the string parameter. The function should employ a single while loop to check the validity of dimension and prompt the user to re-enter the dimension when it is wrong. There should not be any selection structures in this function. Your error message should what dimension is wrong be similar to

The length of the box cannot be less than zero. Please enter the length of the box again.

You should only have one function other than main. Do not use and string objects to store the descriptions of the dimensions, rather send a literal constant to the function.

The code of the original lab (Lab 1-3 Makeup) is below

-----------------------------------------------------

# include

using namespace std;

const double PI = 3.14159;

int main()

{

char shape;

double radius, height, length, width, volume;

cout << "Enter the shape for which you want to calculate the volume. "

<< "use s for sphere, c for cylinder, or b for box ." << endl;

cin >> shape;

//check of input

while (shape != 's' && shape != 'S' && shape != 'c' && shape != 'C'

&& shape != 'b' && shape != 'B')

{

cout << "Your entry was invalid!" << endl;

cout << "Enter the shape for which you want to calculate the volume. "

<< "use s for sphere, c for cylinder, or b for box ." << endl;

cin >> shape;

}

//switch to calculate volume

switch (shape)

{

case 's':

case 'S':

cout << "Enter the radius of the sphere. ";

cin >> radius;

//check of input

while (radius < 0)

{

cout << "The radius of the sphere cannot be less than zero."

<< "Please enter the radius of the sphere again. ";

cin >> radius;

}

volume = 4.0 / 3 * PI * radius * radius * radius;

cout << "The volume of a sphere with radius of " << radius

<< " has a volume of " << volume << ". ";

break;

case 'c':

case 'C':

cout << "Enter the radius of the cylinder. ";

cin >> radius;

//check input

while (radius < 0)

{

cout << "The radius of the cylinder cannot be less than zero. "

<< "Please enter radius of the cylinder again. ";

cin >> radius;

}

cout << "Enter the height of the cylinder. ";

cin >> height;

//check input

while (height < 0)

{

cout << "The height of the cylinder cannot be less than zero. "

<< "Please enter height of the cylinder again. ";

cin >> height;

}

volume = PI * radius * radius * height;

cout << "The volume of a cylinder with radius of " << radius

<< " and height of " << height << " has a volume of " << volume << ". ";

break;

default:

cout << "Enter the height of the box. ";

cin >> height;

//check input

while (height < 0)

{

cout << "The height of the box cannot be less than zero. "

<< "Please enter height of the box again. ";

cin >> height;

}

cout << "Enter the length of the box. ";

cin >> length;

//check input

while (length < 0)

{

cout << "The length of the box cannot be less than zero. "

<< "Please enter length of the box again. ";

cin >> length;

}

cout << "Enter the width of the box. ";

cin >> width;

//check input

while (width < 0)

{

cout << "The width of the box cannot be less than zero. "

<< "Please enter width of the box again. ";

cin >> length;

}

volume = height * length * width;

cout << "The volume of a box with a height of " << height << ", a lengthe of "

<< length << " and width of " << width << " has a volume of " << volume

<< ". ";

}

return 0;

}

Thanks for the help :)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!