. Specifications: Create a menu-driven program that finds and displays areas of
ID: 3847148 • Letter: #
Question
.
Specifications:
Create a menu-driven program that finds and displays areas of 3 different objects.
The menu should have the following 4 choices:
1 -- square
2 -- circle
3 -- right triangle
4 -- quit
If the user selects choice 1, the program should find the area of a square.
If the user selects choice 2, the program should find the area of a circle.
If the user selects choice 3, the program should find the area of a right triangle.
If the user selects choice 4, the program should quit without doing anything.
If the user selects anything else (i.e., an invalid choice) an appropriate error message should be printed.
Sample Run
Program to calculate areas of objects
1 -- square
2 -- circle
3 -- right triangle
4 -- quit
2
Radius of the circle: 3.0
Area = 28.2743
Step 1: Once you have your program working, run it 4 times, each time testing a different valid menu choice. Then run it a fifth time using an invalid menu choice (such as 0 or 5). Make sure your program works correctly for all cases.
Specifications:
Create a menu-driven program that finds and displays areas of 3 different objects.
The menu should have the following 4 choices:
1 -- square
2 -- circle
3 -- right triangle
4 -- quit
If the user selects choice 1, the program should find the area of a square.
If the user selects choice 2, the program should find the area of a circle.
If the user selects choice 3, the program should find the area of a right triangle.
If the user selects choice 4, the program should quit without doing anything.
If the user selects anything else (i.e., an invalid choice) an appropriate error message should be printed.
Sample Run
Program to calculate areas of objects
1 -- square
2 -- circle
3 -- right triangle
4 -- quit
2
Radius of the circle: 3.0
Area = 28.2743
Explanation / Answer
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int choice;
cout<<"Program to calculate areas of objects: "<<endl;
cout<<"1 -- square 2 -- circle 3 -- right triangle 4 -- quit ";
cin >> choice;
double area,side1,radius,side2;
if(choice == 1) {
cout<<"Radius of the circle: ";
cin >> radius;
area = radius * radius;
cout<<"Area = "<<area<<endl;
} else if(choice == 2){
cout<<"Radius of the circle: ";
cin >> radius;
area = M_PI * radius * radius;
cout<<"Area = "<<area<<endl;
} else if(choice == 3){
cout<<"Side1 of the triangle: ";
cin >> side1;
cout<<"Side2 of the triangle: ";
cin >> side2;
area = (side1 * side2)/2;
cout<<"Area = "<<area<<endl;
} else if(choice == 4){
cout<<"bye"<<endl;
} else {
cout<<"an Invalid choice."<<endl;
}
return 0;
}
Output:
sh-4.2$ g++ -o main *.cpp
sh-4.2$ main
Program to calculate areas of objects:
1 -- square
2 -- circle
3 -- right triangle
4 -- quit
2
Radius of the circle: 3.0
Area = 28.2743
sh-4.2$ main
Program to calculate areas of objects:
1 -- square
2 -- circle
3 -- right triangle
4 -- quit
1
Radius of the circle: 2
Area = 4
sh-4.2$ main
Program to calculate areas of objects:
1 -- square
2 -- circle
3 -- right triangle
4 -- quit
3
Side1 of the triangle: 2
Side2 of the triangle: 4
Area = 4
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.