fork download
  1. //Amari Mosley CSC5 Chapter 4, P.224 #21
  2. //
  3. /**************************************************************
  4. *
  5. * Geometry Calculator
  6. * ____________________________________________________________
  7. * This program displays calculates the area of a circle,
  8. * rectangle, or triangle, or quit.
  9. *
  10. * Computation is based on standard geometric formulas:
  11. * Circle Area = 3.14159 * r^2
  12. * Rectangle Area = length * width
  13. * Triangle Area = base * height * 0.5
  14. * ____________________________________________________________
  15. * INPUT
  16. * choice : Menu selection (1-4)
  17. * radius : Radius of the circle
  18. * length : Length of the rectangle
  19. * width : Width of the rectangle
  20. * base : Base of the triangle
  21. * height : Height of the triangle
  22. *
  23. * OUTPUT
  24. * area : Calculated geometric area
  25. *
  26. **************************************************************/
  27.  
  28. #include <iostream>
  29. #include <iomanip>
  30. using namespace std;
  31.  
  32. // Defining Main Function
  33. int main ( )
  34. {
  35. // Defining Constants and Variables
  36. const double PI = 3.14159;
  37. int choice;
  38. double radius;
  39. double length;
  40. double width;
  41. double base;
  42. double height;
  43. double area;
  44.  
  45. // Displaying Menu
  46. cout << "Geometry Calculator\n";
  47. cout << "1. Calculate the Area of a Circle\n";
  48. cout << "2. Calculate the Area of a Rectangle\n";
  49. cout << "3. Calculate the Area of a Triangle\n";
  50. cout << "4. Quit\n";
  51. cout << "Enter your choice (1-4): ";
  52. cin >> choice;
  53.  
  54. // Processing User Choice
  55. switch (choice)
  56. {
  57. case 1:
  58. // Area of Circle
  59. cout << "Enter the radius of the circle: ";
  60. cin >> radius;
  61. if (radius < 0)
  62. {
  63. cout << "Error: Radius cannot be negative." << endl;
  64. }
  65. else
  66. {
  67. area = PI * radius * radius;
  68. cout << "The area of the circle is " << area << endl;
  69. }
  70. break;
  71.  
  72. case 2:
  73. // Area of Rectangle
  74. cout << "Enter the length of the rectangle: ";
  75. cin >> length;
  76. cout << "Enter the width of the rectangle: ";
  77. cin >> width;
  78. if (length < 0 || width < 0)
  79. {
  80. cout << "Error: Length and width cannot be negative." << endl;
  81. }
  82. else
  83. {
  84. area = length * width;
  85. cout << "The area of the rectangle is " << area << endl;
  86. }
  87. break;
  88.  
  89. case 3:
  90. // Area of Triangle
  91. cout << "Enter the length of the triangle's base: ";
  92. cin >> base;
  93. cout << "Enter the height of the triangle: ";
  94. cin >> height;
  95. if (base < 0 || height < 0)
  96. {
  97. cout << "Error: Base and height cannot be negative." << endl;
  98. }
  99. else
  100. {
  101. area = base * height * 0.5;
  102. cout << "The area of the triangle is " << area << endl;
  103. }
  104. break;
  105.  
  106. case 4:
  107. // Quit
  108. cout << "Program ending." << endl;
  109. break;
  110.  
  111. default:
  112. // Input Validation for Menu Choice
  113. cout << "Error: Valid choices are 1 through 4." << endl;
  114. }
  115.  
  116. return 0;
  117. }
Success #stdin #stdout 0s 5320KB
stdin
3
5
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 triangle's base: Enter the height of the triangle: The area of the triangle is 17.5