Question
I have written this code based on instructions below, however i am having trouble with this part: Each shape should have one function to calculate
Instructions:
Write a program that will display the following menu:
Cube
Sphere
Prism
Cylinder
Cone
Quit
Your program will allow the user to choose a shape. It will then prompt the user for the dimensions of the shape and display the surface area and volume of that shape. It will then redisplay the menu and allow the user to choose another option until the user chooses option 6.
Requirements
Each shape should have one function to calculate the surface area of the shape and another function to calculate the volume.
Additionally, you must validate the user input for the dimensions of the shape. This includes ensuring the user enters a valid value (see ReadDouble example from class) and verifying the value is greater than or equal to zero.
#include
#include
using namespace std;
const int CUBE=1;
const int SPHERE=2;
const int PRISM=3;
const int CYLINDER=4;
const int CONE=5;
const int QUIT=6;
int main()
{
int menuChoice=0;
// print menu
cout
cin >> menuChoice;
while (menuChoice != QUIT) {
// Menu choice for cube
if (menuChoice==CUBE) {
double side=0.0;
cout
cin >> side;
while (side
cout = 0.0 \";
cout
cin >> side;
}
double volume=pow(side,3.0);
double sa= (6*pow(side,2.0));
cout
cout
}
// Menu choice for sphere
if (menuChoice==SPHERE) {
double radius=0.0;
cout
cin >> radius;
while (radius
cout = 0.0 \";
cout
cin >> radius;
}
double volume=4.0 / 3.0 * M_PI * pow(radius,3.0);
double sa=(4*M_PI*pow(radius,3.0))/(3);
cout
cout
}
// Menu choice for prism
if (menuChoice==PRISM) {
double legth=0.0, width=0.0,height=0.0;
cout
cin >> legth;
cout
cin >> width;
cout
cin >> height;
while (legth
cout = 0.0 \";
cout
cin >> legth;
}
while (width
cout = 0.0 \";
cout
cin >> width;
}
while (height
cout = 0.0 \";
cout
cin >> legth;
}
double volume= (width*legth*height);
double sa= 2* ((width*legth)+(legth*height)+(width*height));
cout
cout
}
if (menuChoice==CYLINDER) {
double height=0.0, radius=0.0, volume=0.0, sa=0.0;
cout
cin >> height;
while (height
cout = 0.0 \";
cout
cin >> height;
}
cout
cin >> radius;
while (radius
cout = 0.0 \";
cout
cin >> radius;
}
volume=M_PI*pow(radius,2.0)*height;
sa=(2*M_PI*radius*height)+(2*M_PI*radius*radius);
cout
cout
}
// Menu choice for cone
if (menuChoice==CONE) {
double height=0.0, radius=0.0, valume=0.0, sa=0.0;
cout
cin >> height;
cout
cin>> radius;
while (height
cout = 0.0 \";
cout
cin >> height;
}
while (radius
cout = 0.0 \";
cout
cin >> radius;
}
double volume=M_PI*pow( radius,2.0)*(height/3);
cout
sa=(M_PI)*(radius)*(radius+sqrt( pow(height,2.0)+ pow(radius,2.0)));
cout
}
if (menuChoice != CUBE && menuChoice != SPHERE && menuChoice != PRISM && menuChoice != CYLINDER && menuChoice != CONE) {
cout
}
// print menu
cout
cin >> menuChoice;
} // while (menuChoice != QUIT)
cout
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started