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