fork download
  1. //Jacob Silvestre CSC5 Chapter 4, P. 225, #21
  2. //
  3. /************************************************************************************
  4. *
  5. * GEOMETRY CALCULATOR
  6. *____________________________________________________________________________________
  7. * This program displays a geometry calculator menu and allows the user to calculate
  8. * the area of a circle, rectangle, or triangle.
  9. *
  10. * Circle Area Formula:
  11. * Area = 3.14159 x Radius x Radius
  12. *
  13. * Rectangle Area Formula:
  14. * Area = Length x Width
  15. *
  16. * Triangle Area Formula:
  17. * Area = Base x Height x 0.5
  18. * ____________________________________________________________________________________
  19. * INPUT
  20. * choice : Menu choice chosen by user
  21. * radius : Radius of circle
  22. * length : Length of rectangle
  23. * width : Width of rectangle
  24. * base : Base of triangle
  25. * height : height of triangle
  26. *
  27. * OUTPUT
  28. * area : Calculated area of selected shape
  29. *
  30. **************************************************************************************/
  31.  
  32. #include <iostream>
  33. using namespace std;
  34.  
  35. int main() {
  36.  
  37. const double PI = 3.14159; // Constant value of pi
  38.  
  39. int choice; // INPUT - Menu selection
  40. double radius; // INPUT - Radius of circle
  41. double length; // INPUT - Length of rectangle
  42. double width; // INPUT - Width of rectangle
  43. double base; // INPUT - Base of triangle
  44. double height; // INPUT - Height of triangle
  45. double area; // OUTPUT - Area of chosen shape
  46.  
  47. //
  48. // Display Geometry calculator Menu
  49. //
  50.  
  51. cout << "Geometry Calculator" << endl;
  52. cout << "1. Calculate the Area of a Circle" << endl;
  53. cout << "2. Calculate the Area of a Rectangle" << endl;
  54. cout << "3. Calculate the Area of a Triangle" << endl;
  55. cout << "4. Quit" << endl;
  56. cout << "Enter your choice (1-4): ";
  57. cin >> choice;
  58.  
  59. //
  60. // Process User Selection
  61. //
  62. switch(choice)
  63. {
  64. case 1:
  65.  
  66. //
  67. // Input Circle Radius
  68. //
  69. cout << "Enter the radius of the circle: ";
  70. cin >> radius;
  71.  
  72. //
  73. // Validate Radius
  74. //
  75. if (radius < 0)
  76. {
  77. cout << "ERROR: Radius cannot be negative." << endl;
  78. }
  79. else
  80. {
  81.  
  82. //
  83. // Compute Circle Area
  84. //
  85. area = PI * radius * radius;
  86.  
  87. //
  88. // Output Result
  89. //
  90. cout << "The area of the circle is "
  91. << area << endl;
  92. }
  93.  
  94. break;
  95.  
  96. case 2:
  97.  
  98. //
  99. // Input Rectangle Dimensions
  100. //
  101. cout << "Enter the length of the rectangle: ";
  102. cin >> length;
  103.  
  104. cout << "Enter the width of the rectangle: ";
  105. cin >> width;
  106.  
  107. //
  108. // Validate Rectangle Dimensions
  109. //
  110. if (length < 0 || width < 0)
  111. {
  112. cout << "ERROR: Length and width cannot be negative."
  113. << endl;
  114. }
  115. else
  116. {
  117.  
  118. //
  119. // Compute Rectangle Area
  120. //
  121. area = length * width;
  122.  
  123. //
  124. // Output Result
  125. //
  126. cout << "The area of the rectangle is "
  127. << area << endl;
  128. }
  129.  
  130. break;
  131.  
  132. case 3:
  133.  
  134. //
  135. // Input Triangle Dimensions
  136. //
  137. cout << "Enter the base of the triangle: ";
  138. cin >> base;
  139.  
  140. cout << "Enter the height of the triangle: ";
  141. cin >> height;
  142.  
  143. //
  144. // Validate Triangle Dimensions
  145. //
  146. if (base < 0 || height < 0)
  147. {
  148. cout << "ERROR: Base and height cannot be negative."
  149. << endl;
  150. }
  151. else
  152. {
  153.  
  154. //
  155. // Compute Triangle Area
  156. //
  157. area = base * height * 0.5;
  158.  
  159. //
  160. // Output Result
  161. //
  162. cout << "The area of the triangle is "
  163. << area << endl;
  164. }
  165.  
  166. break;
  167.  
  168. case 4:
  169.  
  170. //
  171. // End Program
  172. //
  173. cout << "Program ending." << endl;
  174. break;
  175.  
  176. default:
  177.  
  178. //
  179. // Output Error Message
  180. //
  181. cout << "ERROR: Enter a number from 1 through 4." << endl;
  182.  
  183. }
  184.  
  185. return 0;
  186. }
Success #stdin #stdout 0s 5316KB
stdin
1
5
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 radius of the circle: The area of the circle is 78.5397