Write a C++ program that will display the following menu until the exit choice i
ID: 3544994 • 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>
using namespace std;
const float PI = 3.14;
float calcArea(float num,char ch)
{
if(ch=='c' || ch=='C')
return PI*num*num;
else if(ch=='s' || ch=='S')
return num*num;
}
float calcPerim(float num,char ch)
{
if(ch=='c' || ch=='C')
return 2*PI*num;
else if(ch=='s' || ch=='S')
return 4*num;
}
int main()
{
int choice;
float num;
char ch;
do{
cout<<"Choose one of the following options:"<<endl;
cout<<"Press 1 to enter the side or radius and shape code."<<endl;
cout<<"Press 2 to display the area."<<endl;
cout<<"Press 3 to display the perimeter or circumference."<<endl;
cout<<"Press 4 to exit."<<endl;
cout<<"Enter selection: ";
cin>>choice;
if(choice==1)
{
cout<<"Please enter a value for radius or side: ";
cin >>num;
cout<<"Please enter the shape code (c/C for circle, s/S for square): " ;
cin>>ch;
}
if(choice==2)
{
if(ch=='S' || ch=='s')
cout<<"The area of a square with a side of "<<num<<" is: "<< calcArea(num,'S')<<endl;
if(ch=='c' || ch=='C')
cout<<"The area of a circle with a radius of "<<num<<" is: "<<calcArea(num,'C')<<endl;
}
if(choice==3)
{
if(ch=='S' || ch=='s')
cout<<"The perimeter of a square with a side of "<<num<<" is: "<<calcPerim(num,'S')<<endl;
if(ch=='c' || ch=='C')
cout<<"The perimeter of a circle with a radius of "<<num<<" is: "<<calcPerim(num,'C')<<endl;
}
}while(choice!=4);
cout<<"Thank you for using the geometric calculator. Bye!"<<endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.