fork download
  1. //Matthew Santos CS1A Ch. 4, Pg. 225, #21
  2. /***********************************************
  3.  *
  4.  * CALCULATE AREAS
  5.  * _____________________________________________
  6.  * DESCRIPTION
  7.  * From a selection of different shapes, calculates
  8.  * the area of that selected shape.
  9.  * _____________________________________________
  10.  * INPUT
  11.  * length: length of rectangle
  12.  * width: width of rectangle
  13.  * radius: radius of circle
  14.  * base: width of base of triangle
  15.  * height: height of triangle
  16.  *
  17.  * OUTPUT
  18.  * areaR: area of the rectangle
  19.  * areaC: area of the circle
  20.  * areaT: area of the triangle
  21.  ***********************************************/
  22. #include <iostream>
  23. using namespace std;
  24.  
  25. int main() {
  26.  
  27. //Initialize variables
  28. float length;
  29. float width;
  30. float radius;
  31. float base;
  32. float height;
  33. int choice;
  34. float areaR;
  35. float areaC;
  36. float areaT;
  37.  
  38. //Display menu
  39. cout << "1. Calculate the area of a circle" << endl;
  40. cout << "2. Calculate the area of a rectangle" << endl;
  41. cout << "3. Calculate the area of a triangle" << endl;
  42. cout << "4. Quit" << endl;
  43.  
  44. //Choose
  45. cin >> choice;
  46.  
  47. //Output according to choice
  48. switch (choice){
  49. case 1:
  50. cout << "Enter radius" << endl;
  51. cin >> radius;
  52. areaC = 3.14159 * (radius * radius);
  53. cout << areaC;
  54. break;
  55. case 2:
  56. cout << "Enter length and width" << endl;
  57. cin >> length;
  58. cin >> width;
  59. areaR = length * width;
  60. cout << areaR;
  61. break;
  62. case 3:
  63. cout << "Enter base and height" << endl;
  64. cin >> base;
  65. cin >> height;
  66. areaT = base * height * .5;
  67. cout << areaT;
  68. break;
  69. case 4:
  70. break;
  71. }
  72.  
  73. return 0;
  74. }
Success #stdin #stdout 0s 5316KB
stdin
2
4
5
stdout
1. Calculate the area of a circle
2. Calculate the area of a rectangle
3. Calculate the area of a triangle
4. Quit
Enter length and width
20