Write a menu driven program in C++ (that means Switch statements) that displays
ID: 3674316 • Letter: W
Question
Write a menu driven program in C++ (that means Switch statements) that displays the following menu:
Geometry Calculator
1. Calculate the Area of a Circle
2. Calculate the Area of a Rectangle
3. Calculate the Area of a Triangle
4. Quit Enter your choice (1- 4):
If the user enters 1, the program should ask for the radius of the circle and then display its area.
Use the following formula: Area = p * r2 Use 3.14159 for p and the radius of the circle for r.
If the user enters 2, the program should ask for the length and width of the rectangle and then display the rectangle’s area.
Use the following formula: Area = length * width If the user enters 3 the program should ask for the length of the triangle’s base and its height, and then display its area.
Use the following formula: Area = base * height * 0.5 If the user enters 4, the program should end.
Input Validation: - Display an error message if the user enters a number outside the range of 1 through 4 when selecting an item from the menu. Keep showing him the menu and ask for a valid choice till he enters one.
- Also, do not accept negative values for the circle’s radius, the rectangle’s length or width, or the triangle’s base or height. Keep asking him to enter a valid value for that invalid value.
Explanation / Answer
#include<iostream>
#include<cmath>
using namespace std;
constexpr double pi()
{
return std::atan(1)*4;
}
int get_choice()
{
int choice;
cout << "Geometry Calculator" << ' '<< ' '
<< "1. Calculate the Area of a Circle" << ' '
<< "2. Calculate the Area of a Rectange" << ' '
<< "3. Calculate the Area of a Triangle" << ' '
<< "4. Quit" << ' '
<< ' '
<< "Enter your choice (1-4):" << ' ';
cin >> choice;
return choice;
}
double circle()
{
double radius;
double area;
cout << "What is the radius of your circle?" << ' ';
cin >> radius;
area = pow(radius, 2) * pi();
if(radius < 0)
{
return -1;
}
else
{
return area;
}
}
double rectangle() {
double length;
double width;
double area;
cout << "Enter the length and width of your rectangle, seperated by a "
<< "carriage return." << ' ';
cin >> length >> width;
area = length * width;
if((length < 0) || (width < 0))
{
return -1;
}
else
{
return area;
}
}
double triangle()
{
double base;
double height;
double area;
cout << "Enter the length of the base and the height of your triangle, "<< "seperated by a carriage return." << ' ';
cin >> base >> height;area = 0.5 * base * height;
if((base < 0) || (height < 0))
{
return -1;
}
else
{
return area;
}
}
int check_errors(double input, double area)
{
if((input < 1) || (input > 4))
{
cout << "Error: you entered an invalid choice";
return -1;
}
else if(area < 0)
{
cout << "Error: you entered a negative number";
return -1;
}
else return 0;
}
int main()
{
int input = get_choice();
double area;
switch(input)
{
case 1:
area = circle();
break;
case 2:
area = rectangle();
break;
case 3:
area = triangle();
break;
case 4:
return 0;
default:
break;
}
if(check_errors(input, area) == -1) return 0;
else
{
cout << "The area of your figure is " << area << " units squared." << ' ';
return 0;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.