fork download
  1. //Nicolas Ruano CS1A Chapter 4, Pp. 225, #21
  2. //
  3. /******************************************************************************
  4.  *
  5.  * GEOMETRY CALCULATOR
  6.  * ____________________________________________________________________________
  7.  * This program calculates the area of a circle, rectangle, and triangle.
  8.  *
  9.  *
  10.  * FORMULA IS BASED ON THIS CALCULATION:
  11.  * Area of a cirlce = pi * radius^2
  12.  * Area of rectangle = Base X Height
  13.  * Area of a triangle = Base * Height * .5
  14.  * ____________________________________________________________________________
  15.  * INPUT
  16.  * radiusCircle : The radius of the circle
  17.  * lengthRectangle : The length of the rectangle
  18.  * widthRectangle : The width of the rectangle
  19.  * baseTriangle : The base of the triangle
  20.  * heightTriangle : The height of the triangle
  21.  *
  22.  * OUTPUT
  23.  * areaOfCircle
  24.  * AreaOfRectangle
  25.  * AreaOfTriangle
  26. *******************************************************************************/
  27. #include <iostream>
  28. #include <iomanip>
  29. #include <cmath>
  30. using namespace std;
  31.  
  32. int main() {
  33. int choice; //INPUT - THE CHOICES TO BE MADE 1-4
  34. float radiusCircle; //INPUT - THE RADIUS OF THE CIRCLE
  35. float lengthRectangle; //INPUT - THE LENGTH OF THE RECTANGLE
  36. float widthRectangle; //INPUT - THE WIDTH OF THE RECTANGLE
  37. float baseTriangle; //INPUT - THE BASE OF THE TRIANGLE
  38. float heightTriangle; //INPUT - THE HEIGHT OF THE TRIANGLE
  39. float areaOfCircle; //OUTPUT - THE AREA OF THE CIRCLE TO BE COMPUTED
  40. float areaOfRectangle; //OUTPUT - THE AREA OF THE RECTANGEL TO BE COMPUTED
  41. float areaOfTriangle; //OUTPUT - THE AREA OF THE TRIANGLE TO BE COMPUTED
  42.  
  43. //
  44. // Initialize the variables
  45. cout << "Geometry Calculator" << endl;
  46. cout << "1. Calculate the Area of a Circle" << endl;
  47. cout << "2. Calcualte the Area of a Rectangle" << endl;
  48. cout << "3. Calcualte the area of a Triangle" << endl;
  49. cout << "4. Quit" << endl;
  50. cin >> choice;
  51.  
  52. cout << fixed << setprecision(2) << endl;
  53. if (choice >= 0 and choice <=4) {
  54. switch (choice) {
  55. case 1:
  56. cout << "Please enter the radius of the circle " << endl;
  57. cin >> radiusCircle;
  58. if (radiusCircle > 0) {
  59. areaOfCircle = 3.14159 * (pow(radiusCircle, 2));
  60. cout << "The area of the circle is " << areaOfCircle;
  61. }
  62. else {
  63. cout << "Cannot have negative radius " << endl;
  64. }
  65. break;
  66. case 2:
  67. cout << "Please enter the length of the rectangle " << endl;
  68. cin >> lengthRectangle;
  69. if (lengthRectangle < 0 ) {
  70. cout << "Cannot have a negative length " << endl;
  71. break;
  72. }
  73.  
  74. cout << "Please enter the width of the rectangle " << endl;
  75. cin >> widthRectangle;
  76. if (widthRectangle < 0 ) {
  77. cout << "Cannot have a negative width " << endl;
  78. break;
  79. }
  80. else {
  81. areaOfRectangle = lengthRectangle * widthRectangle;
  82. cout << "The area of the of the rectangle is " << areaOfRectangle;
  83. }
  84. break;
  85. case 3:
  86. cout << "Please enter the base of the triangle " << endl;
  87. cin >> baseTriangle;
  88. if (baseTriangle < 0) {
  89. cout << "Cannot have a negative base " << endl;
  90. break;
  91. }
  92. cout << "Please enter the height of the triangle " << endl;
  93. cin >> heightTriangle;
  94. if (heightTriangle < 0) {
  95. cout << "Cannot have a negative height" << endl;
  96. break;
  97. }
  98. else {
  99. areaOfTriangle = baseTriangle * heightTriangle * .5;
  100. cout << "The area of the triangle is " << areaOfTriangle << endl;
  101. }
  102. break;
  103. case 4:
  104. cout << "Goodbye" << endl;
  105. break;
  106.  
  107.  
  108.  
  109. }
  110. }
  111. else {
  112. cout << "INVALID MENU OPTION " << endl;
  113. }
  114.  
  115.  
  116. return 0;
  117. }
  118.  
Success #stdin #stdout 0.01s 5320KB
stdin
Standard input is empty
stdout
Geometry Calculator
1. Calculate the Area of a Circle
2. Calcualte the Area of a Rectangle
3. Calcualte the area of a Triangle
4. Quit

INVALID MENU OPTION