/*Date: 02/11/08 Programming Fundamentals II Homework Assignment Chapter 4-Question #4.15 Geometry Calculater - Write a program that displays the following menu: Geometry Calculater 1. Calculate the Area of a Circle 2. Calculate the Area of a Rectangle 3. Calculate the Area of a Triangle 4. Quit Enter you Choice (1-4): If the user enters 1, the program should ask for the radius of the circle and then display its area. Use 3.14159 for pie. If the user enters 2, the program should ask for the length and width of the rectangle, and then display the rectangle's area. 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. If the user enters 4, the program should end. Input Validation: Display an error message if the users enters a number outside the range of 1 through 4 when selecting an item from the menu. Do not accept negative values for the circle's radius, the rectangle's length or width, or the triangle's base or height. */ #include #include using namespace std; int main() { //declare variables for measurements input by user double CircleRadius =0, CirclePie =3.14159, RectangleLength = 0, RectangleWidth = 0, TriangleBase =0, TriangleHeight =0, CircleArea =0, RectangleArea=0, TriangleArea=0; int choice;//create variable to hold menu choice //show user the menu cout << "========================================\n"; cout << "Welcome to Hector's Geometry Calculator!\n"; cout << "========================================\n"; cout << "1. Calculate the Area of a Circle\n"; cout << "2. Calculate the Area of a Rectangle\n"; cout << "3. Calculate the Area of a Triangle\n"; cout << "4. Quit\n\n"; cout << "Enter your choice (1-4):\n"; cin >> choice; //take user's input for choice and ask for measurements for object if (choice >= 1 && choice <=4)//if choice between 1-4 { cout <<"Thank you for your selection.\n"; switch (choice) { case 1: //user chooses option 1, calculate area of circle cout <<"Enter the Radius of the Circle.\n"; cin >> CircleRadius; if (CircleRadius < 0)//input cannot be negative number { cout <<"You cannot enter a negative number!\n"; cout << "Please run the program again.\n"; } else { CircleArea = CircleRadius * CircleRadius * CirclePie; cout <<"The area of the circle is " << fixed << setprecision(3) << CircleArea << ".\n"; } break; case 2://user chooses option 2, calculate area of rectangle cout <<"Enter the Length of the Rectangle.\n"; cin >> RectangleLength; if (RectangleLength < 0)//input cannot be negative number { cout <<"You cannot enter a negative number!\n"; cout << "Please run the program again.\n"; } cout <<"Enter the Width of the Rectangle.\n"; cin >> RectangleWidth; if (RectangleWidth < 0)//input cannot be negative number { cout <<"You cannot enter a negative number!\n"; cout << "Please run the program again.\n"; } else { RectangleArea = RectangleLength * RectangleWidth; cout <<"The area of the rectangle is " << fixed << setprecision(3) << RectangleArea << ".\n"; } break; case 3://user chooses option 3, calculate area of triangle cout <<"Enter the Length of the Base of the Triangle.\n"; cin >> TriangleBase; if (TriangleBase < 0)//input cannot be negative number { cout <<"You cannot enter a negative number!\n"; cout << "Please run the program again.\n"; } cout <<"Enter the Height of the Triangle.\n"; cin >> TriangleHeight; if (TriangleHeight < 0)//input cannot be negative number { cout <<"You cannot enter a negative number!\n"; cout << "Please run the program again.\n"; } else { TriangleArea = (TriangleBase * .5) * TriangleHeight; cout <<"The area of the rectangle is " << fixed << setprecision(3) << TriangleArea << ".\n"; } break; } } else if (choice != 4)//if users choice not 1-4, terminate program { cout << "ERROR! The valid choices are only 1-4!\n"; cout << "Please run the program again.\n"; } return 0; }