fork download
  1. //Henry Hill CSC5 Chapter 4, P. 226, #25
  2. //
  3. /**************************************************************
  4.  * CALCULATE AREA OF CIRCLE, TRIANGLE, OR RECTANGLE
  5.  * ____________________________________________________________
  6.  * This program will ask the user to choose either a circle
  7.  * rectangle, or triangle, and will then ask for the components
  8.  * necessary to calculate and display the area of either
  9.  * requested shape.
  10.  * ____________________________________________________________
  11.  * INPUT
  12.  * radius : Radius of a cirlce
  13.  * length : Length of a rectangle
  14.  * width : Width of a rectangle
  15.  * base : Base of a triangle
  16.  * height : Height of a triangle
  17.  * shapeChoice : User's menu choice
  18.  *
  19.  * OUTPUT
  20.  * areaCircle : Area of the circle
  21.  * areaRectangle : Area of the rectangle
  22.  * areaTriangle : Area of the triangle
  23.  ****************************************************************/
  24.  
  25. #include <iostream>
  26. #include <iomanip>
  27. #include <cmath>
  28. using namespace std;
  29.  
  30. int main()
  31. {
  32. const float PI = 3.14159;
  33. const int shapeChoice1 = 1;
  34. const int shapeChoice2 = 2;
  35. const int shapeChoice3 = 3;
  36. const int shapeChoice4 = 4;
  37. float radius; //
  38. float length; //
  39. float width; //
  40. float base; //
  41. float height; //
  42. int shapeChoice; //
  43. float areaCircle; //
  44. float areaRectangle; //
  45. float areaTriangle; //
  46.  
  47. // Menu bit
  48. cout << "Geometry Calculator\n";
  49. cout << "1. Calculate the Area of a Circle\n";
  50. cout << "2. Calculate the Area of a Rectangle\n";
  51. cout << "3. Calculate the Area of a Triangle\n";
  52. cout << "4. Quit\n";
  53.  
  54. cin >> shapeChoice;
  55.  
  56. cout << fixed;
  57. cout << showpoint;
  58. cout << setprecision(2);
  59.  
  60. if (shapeChoice == shapeChoice1)
  61. {
  62. cout << "\nWhat is the radius? ";
  63. cin >> radius;
  64. areaCircle = PI * pow(radius, 2);
  65. cout << "\nThe area of the circle is " << areaCircle;
  66. }
  67. else if (shapeChoice == shapeChoice2)
  68. {
  69. cout << "\nWhat is the length and width of the rectangle?";
  70. cin >> length;
  71. cin >> width;
  72. areaRectangle = length * width;
  73. cout << "\nThe area of the Rectangle is " << areaRectangle;
  74. }
  75. else if (shapeChoice == shapeChoice3)
  76. {
  77. cout << "\nWhat is the length and width of the triangle?";
  78. cin >> base;
  79. cin >> height;
  80. areaTriangle = base * height * 0.5;
  81. cout << "\nThe area of the triangle is " << areaTriangle;
  82. }
  83. else if (shapeChoice == shapeChoice4)
  84. {
  85. cout << "\nDone";
  86. }
  87. else
  88. cout << "Invalid ";
  89.  
  90. return 0;
  91. }
  92.  
Success #stdin #stdout 0.01s 5464KB
stdin
1
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

What is the radius? 
The area of the circle is 314.16