In: Computer Science
Instructions
The included program found in main.cpp is designed to find the area of a rectangle, the area of a circle, or the volume of a cylinder.
However:
Rewrite the program so that it works correctly. Your program must be properly indented. (Note that the program is menu driven and allows the user to run the program as long as the user wishes.)
Format your output with setprecision(2) to ensure the proper number of decimals for testing!
I have tried previous answers on site, but they are all incorrect and fail in the Mindtap cite used for the class.
=>Note wrote the c++ program and i used only float for inputs.
Code:-
#include<iostream>
#include <iomanip>
#include <stdlib.h>
using namespace std;
float pi=3.14159;
void rectangle(float height,float width){ // function rectangle
takes height and width and area of the rectangle
float area=height*width;
std::cout<<"Area of rectangle:
"<<std::setprecision(2)<< area<<endl;
}
void circle(float radius){ //function takes radius and prints
area od cirlce
float area=pi*radius*radius;
std::cout<<"Area of circle is: "<<
std::setprecision(2)<< area <<endl;
}
void cylinder(float height,float radius){ // takes radius and
height of the cylinder and prints the volume of cylinder
double vol=pi*radius*radius*height;
std::cout<<"Volume of cylinder is:
"<<std::setprecision(2)<<vol<<endl;
}
int main(){
while(true){
float height;
float width;
float radius;
int a;
cout << "Enter 1-- rectangle\nEnter 2--
circle\nEnter 3--cylinder 4-- quit\n"; //switch is used to select
the option
cin >> a;
switch(a){
case 1:
cout<<"Enter length and width:\n";
cin>>height;
cin>>width;
rectangle(height,width);
break;
case 2:
cout<<"Enter radius:\n";
cin>>radius;
circle(radius);
break;
case 3:
cout<<"Enter radius and height:\n";
cin>>height;
cin>>radius;
cylinder(height,radius);
break;
case 4:
exit(0);
default:
cout<<"Invalid input";// prints if invalid input is
given
break;
}
}
}