Write a program to calculate the area of several basic geometric shapes. The pro
ID: 3620621 • Letter: W
Question
Write a program to calculate the area of several basic geometric shapes. The program first asks the user for a shape ( C = Circle , R = Rectangle , S = Square , and T = Triangle ). The program then calls one of four user defined functions to calculate the area of the shape and return the answer to the main for printing to the screen.Depending on the shape, the corresponding function's argument(s) is (are) the necessary parameter(s) for area calculation. Therefore, the main should receive the value(s) of the parameter(s) from the user before calling any function.
You must test your program for all shapes. The program should reject request for any other shapes; this option must be tested as well.
The following is a list of parameters for each shape and the expression for its area calculation:
Shape Parameters Area Expression
Circle (C) Radius Area = 3.14159(Radius)(Radius)
Square (S) Side Area = (Side)(Side)
Rectangle (R) Height, Width Area = Width(Height)
Triangle (T) Base, Height Area = 0.5 (Base)(Height)
Explanation / Answer
please rate - thanks #include<iostream>using namespace std;
double square();
double rectangle();
double triangle();
double circle();
double get(string);
int main()
{
double area=0;
char input;
bool error;
for(;;)
{cout<<"Enter a shape: ";
cout<<"Circle (C) Square (S) Rectangle (R) Triangle (T) ";
cin>> input;
error=false;
switch(input)
{case 'c':case 'C':
area=circle();
break;
case 's':case 'S':
area=square();
break;
case 'r':case 'R':
area=rectangle();
break;
case 't':case 'T':
area=triangle();
break;
default:
{cout<<"Invalid entry ";
error=true;
}
}
if(!error)
cout<<" Area="<<area<<" ";
}
}
double square()
{double a=get("length");
return a*a;
}
double rectangle()
{double a=get("length");
double b=get("width");
return a*b;
}
double triangle()
{double a=get("base");
double b=get("height");
return (a*b)/2.;
}
double circle()
{double a=get("radius");
return a*a*3.14159;
}
double get(string mess)
{double a;
do
{cout<<"Enter a value for "<<mess<<": ";
cin>>a;
if(a<=0)
cout<<"Invalid entry- redo";
}while(a<=0);
return a;
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.