Write a C++ program that will display the following menu until the exit choice i
ID: 3544995 • Letter: W
Question
Write a C++ program that will display the following menu until the exit choice is selected (See Sample Program Run below). The program will calculate the areas and perimeter or circumference by callingonly two functions and passing two parameters: a numeric value (side), and a character code to each function.
Program Requirements:
1. The character code,
Write a C++ program that will display the following menu until the exit choice is selected (See Sample Program Run below). The program will calculate the areas and perimeter or circumference by callingonly two functions and passing two parameters: a numeric value (side), and a character code to each function. The character code, 'C' or 'S', is used to distinguish between circle and square shapes. calcArea() will accept two parameters and returns the area of the shape. calcPerim() will accept two parameters and returns the perimeter or circumference of the shape. The main() function will display a message which identifies the selected shape and its area or perimeter/circumference, depending upon the selected option. PI should be declared as a constant.Explanation / Answer
#include<iostream>
#include<math.h>
#include<iomanip>
using namespace std;
double calcArea(int sideA, int sideB, char Shape){
if(Shape=='R'||Shape=='r'){
return sideA*sideB;
}
else
return (sideA*sideB*(1/2));
}
double calcPerim(int sideA, int sideB, char Shape){
if(Shape=='R'||Shape=='r'){
return 2*(sideA+sideB);
}
else
return (sideA+sideB+sqrt((sideA*sideA)+(sideB*sideB)));
}
int main(){
int selection;
int side1,side2;
char code;
cout<<setiosflags(ios::fixed)<<setprecision(2);
cout<<"Geometric Calculator";
cout<<" Choose one of the following options: ";
while(1){
cout<<" Press 1 to enter the sides and shape code.";
cout<<" Press 2 to display the area.";
cout<<" Press 3 to display the perimeter.";
cout<<" Press 4 to exit.";
cout<<" Enter selection: ";
cin>>selection;
switch (selection){
case 1:
cout<<"Please enter a value for side 1: ";
cin>>side1;
cout<<"Please enter a value for side 2: ";
cin>>side2;
cout<<"Please enter the shape code(t/T for right triangle, r/R for rectangle): ";
cin>>code;
break;
case 2:
if(code=='r'||code=='R'){
cout<<"The area of a rectangle with sides "<<side1<<" and "<<side2<<" is: ";
cout<<calcArea(side1,side2,code)<<endl;
}
else if(code=='t'||code=='T'){
cout<<"The area of a right triangle with sides "<<side1<<" and "<<side2<<" is: ";
cout<<calcArea(side1,side2,code)<<endl;
}
break;
case 3:
if(code=='r'||code=='R'){
cout<<"The perimeter of a rectangle with sides "<<side1<<" and "<<side2<<" is: ";
cout<<calcPerim(side1,side2,code)<<endl;
}
else if(code=='t'||code=='T'){
cout<<"The perimeter of a right triangle with sides "<<side1<<" and "<<side2<<" is: ";
cout<<calcPerim(side1,side2,code)<<endl;
}
break;
case 4:
cout<<"Thank you for using the geometric calculator. Bye!";
cin.get();
return 0;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.