fork download
  1. //George Molinero csc5 chapter 4, P. 225, #21
  2. //
  3. /*******************************************************************************
  4.  *
  5.  * Compute Area of Selected Shape
  6.  * _____________________________________________________________________________
  7.  * This program computes the area of the shape that is selected from the given
  8.  * menu.
  9.  * _____________________________________________________________________________
  10.  * INPUT
  11.  * choice : Option selection
  12.  * r : radius of cirle
  13.  * length : length of rectangle
  14.  * width : width of rectangle
  15.  * base : base of triangle
  16.  * height : height of triangle
  17.  *
  18.  * OUTPUT
  19.  * calculated Area
  20.  ******************************************************************************/
  21. #include <iostream>
  22. using namespace std;
  23. int main ()
  24. {
  25. const int Circle = 1;
  26. const int Rectangle = 2;
  27. const int Triangle = 3;
  28. const int Quit = 4;
  29. int choice;
  30. float r;
  31. float length;
  32. float width;
  33. float base;
  34. float height;
  35. //
  36. // Display Menu Selection
  37. cout << "Geometry Calculator" << endl << endl;
  38. cout << "1. Calculate the Area of a Circle" << endl;
  39. cout << "2. Calculate the Area of a Rectangle" << endl;
  40. cout << "3. Calculate the Area of a Triangle" << endl;
  41. cout << "4. Quit" << endl << endl;
  42. //
  43. // Ask for selection
  44. cout << "Enter your choice (1-4):" << endl;
  45. cin >> choice;
  46. //
  47. // input Validation
  48. if (choice >= 1 && choice <= 4)
  49. {
  50. switch(choice)
  51. {
  52. case Circle:
  53. cout << "Enter the radius of the circle: " << endl;
  54. cin >> r;
  55. cout << endl;
  56. if(r >= 0)
  57. cout << "The area of the circle is " << 3.14159 * (r * r) << endl;
  58. else
  59. cout << "radius cannot be negative";
  60. break;
  61.  
  62. case Rectangle:
  63. cout << "Enter the length of the rectangle: " << endl;
  64. cin >> length;
  65. cout << "Enter the width of the rectangle: " << endl;
  66. cin >> width;
  67. cout << endl;
  68. if(length >= 0 && width >= 0)
  69. cout << "The area of the rectangle is " << length * width << endl;
  70. else
  71. cout << "Both length and width have to be positive";
  72. break;
  73.  
  74. case Triangle:
  75. cout << "Enter the length of the triagle's base: " << endl;
  76. cin >> base;
  77. cout << "Enter the height of the triangle: " << endl;
  78. cin >> height;
  79. cout << endl;
  80. if(base >= 0 && height >= 0)
  81. cout << "The area of the triangle is " << base * height * 0.5;
  82. else
  83. cout << "Both base and height have to be positive";
  84. break;
  85.  
  86. case Quit:
  87. cout << endl << "Program ending." << endl;
  88. break;
  89. }
  90. }
  91. else
  92. cout << endl << "ERROR: The valid choices are 1 through 4." << endl;
  93. return 0;
  94. }
Success #stdin #stdout 0.01s 5284KB
stdin
2
5
6
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 is 30