Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- #include <cmath>
- using namespace std;
- int main ()
- {
- //Declaration of Variables.
- double circleArea, rectangleArea, triangleArea,
- length, width, height, base, radius;
- char choice;
- const double PI = 3.14159;
- //Display the menu.
- cout << "\tGeometry Calculator" << endl << endl;
- cout << setw(10) << "1. Calculate the Area of a Circle" << endl;
- cout << setw(10) << "2. Calculate the Area of a Rectangle" << endl;
- cout << setw(10) << "3. Calculate the Area of a Triangle" << endl;
- cout << setw(6) << "4. Quit" << endl;
- cout << endl;
- //Prompt User for choice.
- cout << "Enter your choice (1-4)" << endl;
- cin >> choice;
- cout << endl;
- //Calculate the area of a Circle.
- switch (choice)
- {
- case 'C':
- case 'c':
- case '1':
- cout << "Enter the radius of the circle: ";
- cin >> radius;
- if (radius < 0)
- {
- cout << "Number must be greater than 0. Try again" << endl;
- }
- else
- {
- circleArea = PI * pow(radius, 2);
- cout << "The area of the circle is: " << circleArea;
- }
- cout << endl;
- break;
- //Calculate the area of a Rectangle.
- case 'R':
- case 'r':
- case '2':
- cout << "Enter the length and width of the rectangle: ";
- cin >> length >> width;
- if (length < 0 || width < 0)
- {
- cout << "Number must be greater than 0. Try again" << endl;
- }
- else
- {
- rectangleArea = length * width;
- cout << "The area of the rectangle is: " << rectangleArea;
- }
- cout << endl;
- break;
- //Calculate the area of a Triangle.
- case 'T':
- case 't':
- case '3':
- cout << "Enter the base and height of the triangle: ";
- cin >> base >> height;
- if (base < 0 || height < 0)
- {
- cout << "Number must be greater than 0. Try again" << endl;
- }
- else
- {
- triangleArea = base * height * .5;
- cout << "The area of the triangle is: " << triangleArea;
- }
- cout << endl;
- break;
- //Quit the program.
- case 'Q':
- case 'q':
- case '4':
- cout << "End of Program" << endl;
- cout << endl;
- break;
- //Invalid number.
- default:
- cout << "That is an invalid choice." << endl;
- cout << "Please select choices 1-4." << endl;
- break;
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment