Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ Program: Redesign the program that calculates the area of geometric shapes. Create a function call for each selection the user makes. Use these as

C++ Program: Redesign the program that calculates the area of geometric shapes. Create a function call for each selection the user makes.

Use these as the function declaration: //function declaration void showMenu(); double rectangle(double length, double width); double triagle(double base, double height); double circle(double radius);

Here is the source code to calculate the area of geometric shapes:

#include #include //allows decimal points to be shown (setprecision)

using namespace std;

int main() { int choice; //To hold a menu choice double length, //holds the stuff needed to caluclate areas width, base, height, r; double area;

//constants for menu choices const int AREA_RECTANGLE = 1, AREA_TRIANGLE = 2, AREA_CIRCLE = 3, QUIT = 4;

//set up reputation do { //display the menu and get choice cout << "\t\tGeometry Calculator Menu "; cout << "1. Calculate the Area of a Rectangle. "; cout << "2. Calculate the Area of a Triangle. "; cout << "3. Calculate the Area of a Circle. "; cout << "4. Quit. "; cout << "Enter your choice (1, 2, 3, or 4): "; cin >> choice;

//set numeric outpoint formatting cout << fixed << showpoint << setprecision(2);

//respond to the user's menu selection switch (choice) { case AREA_RECTANGLE: //choice 1 - rectangle cout << "Enter the length of the rectangle: "; cin >> length; cout << "Enter the width of the rectangle: "; cin >> width; //only calculate area if user enters positive numbers if ((length > 0) && (width > 0)) { //calculate the area area = length * width; //display the area cout << "The area of the rectangle is " << area << ". "; } else cout << "Please enter positive numbers only. " << endl; break; case AREA_TRIANGLE: //choice 2 - triangle cout << "Enter the base of the triangle: "; cin >> base; cout << "Enter the height of the triangle: "; cin >> height; //only calculate area if user enters positive numbers if ((base > 0) && (height > 0)) { //calculate the area area = base * height *.5; //display the area cout << "The area of the triangle is " << area << ". "; } else cout << "Please enter positive numbers only. " << endl; break; case AREA_CIRCLE: //choice 3 - circle cout << "Enter the radius of the circle: "; cin >> r; //only calculate area if user enters positive numbers if (r > 0) { //calculate the area area = 3.14159 * (r * r); //display the area cout << "The area of the circle is " << area << ". "; } else cout << "Please enter a positive number for the radius. " << endl; break; case QUIT: //choice 4 - quit program cout << "Program will end. "; } } while (choice != QUIT);

return 0; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

8. Describe the steps in the development planning process.

Answered: 1 week ago