fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6. // Constant for pi
  7. const double PI = 3.14159;
  8.  
  9. // Constants for menu choices
  10. const int CIRCLE_CHOICE = 1;
  11. const int RECTANGLE_CHOICE = 2;
  12. const int TRIANGLE_CHOICE = 3;
  13. const int QUIT_CHOICE = 4;
  14.  
  15. int choice; // User's shape choice
  16. double radius; // Radius of a circle
  17. double length; // Length of a rectangle
  18. double width; // Width of a rectangle
  19. double base; // Base of a triangle
  20. double height; // Height of a triangle
  21. double area; // Area of the selected shape
  22.  
  23.  
  24. // Display the menu and get a choice.
  25. cout << "Geometry Calculator\n\n";
  26. cout << "1. Calculate the area of a Circle\n";
  27. cout << "2. Calculate the area of a Rectangle\n";
  28. cout << "3. Calculate the area of a Triangle\n";
  29. cout << "4. Quit\n\n";
  30. cout << "Enter your choice (1-4): ";
  31. cin >> choice;
  32.  
  33. // Respond to the user's menu selection.
  34. switch (choice)
  35. {
  36. case CIRCLE_CHOICE:
  37. cout << "\nEnter the circle's radius: ";
  38. cin >> radius;
  39. if (radius < 0)
  40. cout << "\nThe radius can not be less than zero.\n";
  41. else
  42. {
  43. area = PI * radius * radius;
  44. cout << "\nThe area is " << area << endl;
  45. }
  46. break;
  47.  
  48. case RECTANGLE_CHOICE:
  49. cout << "\nEnter the rectangle's length: ";
  50. cin >> length;
  51. cout << "Enter the rectangle's width: ";
  52. cin >> width;
  53. if (length < 0 || width < 0)
  54. {
  55. cout << "\nOnly enter positive values for "
  56. << "length and width.\n";
  57. }
  58. else
  59. {
  60. area = length * width;
  61. cout << "\nThe area is " << area << endl;
  62. }
  63. break;
  64.  
  65. case TRIANGLE_CHOICE:
  66. cout << "Enter the length of the base: ";
  67. cin >> base;
  68. cout << "Enter the triangle's height: ";
  69. cin >> height;
  70. if (base < 0 || height < 0)
  71. {
  72. cout << "\nOnly enter positive values for "
  73. << "base and height.\n";
  74. }
  75. else
  76. {
  77. area = base * height * 0.5;
  78. cout << "\nThe area is " << area << endl;
  79. }
  80. break;
  81.  
  82. case QUIT_CHOICE:
  83. cout << "Thank you for using the program. Bye...\n";
  84. break;
  85.  
  86. default:
  87. cout << "The valid choices are 1 through 4. Run the\n"
  88. << "program again and select one of those.\n";
  89. }
  90.  
  91. return 0;
  92. }
Success #stdin #stdout 0.01s 5288KB
stdin
3
6
7
stdout
Geometry Calculator

1. Calculate the area of a Circle
2. Calculate the area of a Rectangle
3. Calculate the area of a Triangle
4. Quit

Enter your choice (1-4): Enter the length of the base: Enter the triangle's height: 
The area is 21