Question: C++ Program: Write a program that displays the follwing menu: Geometry Calculator 1. Calculate the Area of a Rectangle 2. Calculate the Area of a

C++ Program: Write a program that displays the follwing menu:

Geometry Calculator 1. Calculate the Area of a Rectangle 2. Calculate the Area of a Triangle 3. Clculate the Area of a Circle 4. Quit Enter your choice (1-4):

User 3.14159 for pi and the radius of the circle for r.

If the user enters 1, the program should ask for the length and width of the rectangle, then display its area. Formula: area = length * width If the user enters 2, the program should ask for the triangle's base and height, then display its area. Formula: area = base * height * .5 If the user enters 3, the program shgould ask for the radius of the circle, then display its area. Formula: area = pi * (r * r) 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. Also, DO NOT accept negative values for the circle's radius, rectangles length or width, and the triangle's base or height.

Here's what I have. (Everything except the input validation -- I'm stuck on that part.)

#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;

//only do calculations if the values are positive numbers (???? NEED HELP) //only do calculations if the selection is between 1 and 4 (???? NEED HELP)

//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 << " 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; //calculate the area area = length * width; //display the area cout << "The area of the rectangle is " << area << ". "; 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; //calculate the area area = base * height *.5; //display the area cout << "The area of the triangle is " << area << ". "; break; case AREA_CIRCLE: //choice 3 - circle cout << "Enter the radius of the circle: "; cin >> r; //calculate the area area = 3.14159 * (r * r); //display the area cout << "The area of the circle is " << area << ". "; 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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!