fork download
  1. // Mariah Contreras CSC5 Chapter 4, P.226, #23
  2. //
  3. /* ************************************************************
  4.  *
  5.  * COMPUTE GEOMETRIC AREAS
  6.  * ____________________________________________________________
  7.  * This program will display a menu of geometry
  8.  * calculations. Then based upon the users selction will
  9.  * compute the area of a specificed shape and display it.
  10.  *
  11.  * Computation is based on the formula :
  12.  * Area of a Circle = PI * (Radius * Radius)
  13.  * Area of a Rectangle = Lenght * Width
  14.  * Area of a Triangle = Base * Height * 0.5
  15.  * ____________________________________________________________
  16.  * INPUT
  17.  * choice : choice on menu
  18.  * radius : Radius of Circle
  19.  * length : Length of Rectangle
  20.  * width : Width of Rectangle
  21.  * base : Base of Triangle
  22.  * height : Height of Triangle
  23.  *
  24.  * OUTPUT
  25.  * area : Area of choosen shape
  26.  *
  27.  **************************************************************/
  28. #include <iostream>
  29. #include <iomanip>
  30. using namespace std;
  31. int main ()
  32. {
  33. float const PI = 3.14159; // CONSTANT - PI
  34. int const areaCircle = 1, // CONSTANT - Menu Choice 1
  35. areaRectangle = 2, // CONSTANT - Menu Choice 2
  36. areaTriangle = 3, // CONSTANT - Menu Choice 3
  37. quit = 4; // CONSTANT - Menu Choice 4
  38. int choice; // INPUT - Menu Choice
  39. float radius, // INPUT - Radius of circle
  40. length, // INPUT - Length of Rectangle
  41. width, // INPUT - Width of Rectangle
  42. base, // INPUT - Base of Triangle
  43. height, // INPUT - Height of Triangle
  44. area; // OUTPUT - Area of Choosen Shape
  45. //
  46. // Display Menu and Prompt User for Choice
  47. cout << "Geometry Calculator\n";
  48. cout << "1. Calculate the Area of a Circle\n";
  49. cout << "2. Calculate the Area of a Rectangle\n";
  50. cout << "3. Calculate the Area of a Triangle\n";
  51. cout << "4. Quit\n";
  52. cout << "Enter your choice (1-4):";
  53. // Get Menu Choice and Respond
  54. cin >> choice;
  55. switch (choice)
  56. {
  57. //
  58. // Area of Circle
  59. case areaCircle:
  60. cout << "\nEnter the Radius of the circle";
  61. cin >> radius;
  62. if (radius > 0)
  63. {
  64. area = PI * (radius * radius);
  65. cout << "\nThe Area of the Circle: " << area;
  66. }
  67.  
  68. else
  69. {
  70. cout << "\nYou Entered an Invalid Number.Please ";
  71. cout << "\nreset the Program and enter a valid number.";
  72. }
  73. break;
  74. //
  75. // Area of Rectangle
  76. case areaRectangle:
  77. cout << "\nEnter the length of the rectangle:";
  78. cin >> length;
  79. cout << "\nEnter the width of the rectangle";
  80. cin >> width;
  81.  
  82. if (length > 0 && width > 0)
  83. {
  84. area = length * width;
  85. cout << "\nThe Area of the Rectangle: " << area;
  86. }
  87. else
  88. {
  89. cout << "\nYou entered an invalid number.Please ";
  90. cout << "\nreset the program and use valid numbers.";
  91. }
  92. break;
  93. //
  94. // Area of Triangle
  95. case areaTriangle:
  96. cout << "\nEnter the base of the triangle:";
  97. cin >> base;
  98. cout << "\nEnter the height of the triangle";
  99. cin >> height;
  100. if (base > 0 && height > 0)
  101. {
  102. area = base * height * 0.5;
  103. cout << "\nThe Area of the Triangle: " << area;
  104. }
  105. else
  106. {
  107. cout << "\nYou entered an invalid number.Please ";
  108. cout << "\nReset the program and use valid numbers.";
  109. }
  110. break;
  111. //
  112. // Quit Program
  113. case quit:
  114. cout << "\nYou've ended the program";
  115. break;
  116. default: cout << "\nMenu input invalid. Please enter a number from 1-4.\n";
  117. }
  118. return 0;
  119. }
Success #stdin #stdout 0.01s 5516KB
stdin
2
10
10
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 rectangle:
Enter the width of the rectangle
The Area of the Rectangle: 100