(Program must be written using C++) Goals: Practicing switch control structures
ID: 3888593 • Letter: #
Question
(Program must be written using C++)
Goals: Practicing switch control structures and while loops
Problem: Write a program to calculate the volume of a sphere, cylinder, or box (a parallelepiped with right angles). The user should enter an “s” or “S” for a sphere, a “c” or “C” for a cylinder, or a “b” or “B” for a box. Use a while loop to confirm that the user entered a correct letter. Depending on the user’s input for shape the user should be prompted for the appropriate dimensions using a switch structure. Validate input for dimensions using a while loop for each entered dimension. Make sure that your while loop to check dimensions outputs what dimension is incorrect (see below for an example message). The while loops to check dimensions should be inside the branches of the switch. None of the while loops should contain selection structures. Then calculate the volume.
Your program should output the shape the user entered, the dimensions he/she entered as well as the calculated volume (Hint look at the demo program that uses a switch for an integer calculator for hints).
Make sure that you have included your introductory comments containing the purpose of the program i.e. what problem or task is being solved plus the input needed from user to complete the problem/task, output expected from the program, and the processing needed to get the output from the input. The description of the purpose should be detailed enough that a person reading your code would not need any additional information to understand the problem. The processing comments should not include any C++ commands.
Explanation / Answer
Hi,
C++ Code-
#include<iostream>
using namespace std;
float vol(int,int);
float vol(float);
int vol(int);
int main()
{
int r,h,a;
float r1;
char ch;
cout<<"Enter your choice to calculate volume(s for sphere, c for cyllinder, b for Box)";
cin>>ch;
do
{
switch(ch){
case 'c': case 'C':
cout<<" Enter radius and height of a cylinder:";
cin>>r>>h;
while(r==0 || h==0){
cout<<" The radius or height value cannot be zero. Please enter the values again";
cin>>r>>h;
}
cout<<" Volume of cylinder is"<<vol(r,h);
break;
case 's': case 'S':
cout<<" Enter radius of sphere:";
cin>>r1;
while(r1==0){
cout<<" The radius value cannot be zero. Please enter the value again";
cin>>r1;
}
cout<<" Volume of sphere is"<<vol(r1);
break;
case 'b': case 'B':
cout<<" Enter side of Box:";
cin>>a;
while(a==0){
cout<<" The length of the box cannot be less than zero. Please enter the length of the box again";
cin>>a;
}
cout<<" Volume of cube is"<<vol(a);
break;
default:
cout<<" The choice was incorrect. Please make correct choice";
cin>>ch;
break;
}
}while(ch=='s'||ch=='S'||ch=='c'||ch=='C'||ch=='b'||ch=='B');
return 0;
}
float vol(int r,int h)
{
return(3.14*r*r*h);
}
float vol(float r1)
{
return((4*3.14*r1*r1*r1)/3);
}
int vol(int a)
{
return(a*a*a);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.