fork download
  1. //Natalie Zarate CIS 5 Chapter 4, P. 222, #21
  2.  
  3. /*******************************************************************************
  4.  *
  5.  * COMPUTE SHAPE AREA
  6.  * _____________________________________________________________________________
  7.  * This program accepts will display a menu of 4 different shape areas the user
  8.  * can prompt the program to calculate. The program, will accept numbers greater
  9.  * than zero and use the user input to calculate the area of the selected shape,
  10.  * using the formulas 2(pi)r^2 , l * w, or (b * h) / 2.
  11.  * _____________________________________________________________________________
  12.  * INPUT
  13.  * numChoice : The choice the user picks from the menu
  14.  * radius : Radius of circle
  15.  * pi : value of pi
  16.  * length : Length of rectangle
  17.  * width : Width of rectangle
  18.  * base : Base of triangle
  19.  * height : Height of triangle
  20.  *
  21.  * OUTPUT
  22.  * area : area of shape
  23.  *
  24.  ******************************************************************************/
  25. #include <iostream>
  26. #include <iomanip>
  27. using namespace std;
  28.  
  29. int main ()
  30. {
  31. /**********************************************************************
  32.   * CONSTANTS
  33.   * --------------------------------------------------------------------
  34.   * pi - value of pi
  35.   * *******************************************************************/
  36.  
  37. const float pi = 3.14159;
  38.  
  39. int numChoice; // INPUT - The choice the user picks from the menu
  40. float radius; // INPUT - Radius of circle
  41. float length; // INPUT - Length of rectangle
  42. float width; // INPUT - Width of rectangle
  43. float base; // INPUT - Base of triangle
  44. float height; // INPUT - Height of triangle
  45. float area; // OUTPUT - Area of shape
  46.  
  47. // Prompt user menu
  48. cout << "Geometry Calculator" << endl;
  49. cout << setw(5) << "1." << " Calculate the Area of a Circle" << endl;
  50. cout << setw(5) << "2." << " Calculate the Area of a Rectangle" << endl;
  51. cout << setw(5) << "3." << " Calculate the Area of a Triangle" << endl;
  52. cout << setw(5) << "4." << " Quit" << endl;
  53. cout << endl;
  54. cout << setw(8) << "Enter" << " your choice (1-4):" << endl;
  55. cin >> numChoice;
  56.  
  57. // Validate input, calculate, and display
  58. switch (numChoice)
  59. {
  60. case 1: cout << "Enter the circle's radius:" << endl;
  61. cin >> radius;
  62. if (radius < 0)
  63. {
  64. cout << "Radius cannot be negative, re-start program and";
  65. cout << " try again.";
  66. }
  67. else
  68. {
  69. area = 2 * pi * radius;
  70. cout << "The area of the circle is " << area << ". \n";
  71. }
  72. break;
  73.  
  74. case 2: cout << "Enter the length of the rectangle:" << endl;
  75. cin >> length;
  76. if (length < 0)
  77. {
  78. cout << "Length cannot be negative, re-start program and";
  79. cout << " try again.";
  80. }
  81. cout << "Enter the width of the rectangle:" << endl;
  82. cin >> width;
  83. if (width < 0)
  84. {
  85. cout << "Width cannot be negative, re-start program and";
  86. cout << " try again.";
  87. }
  88. else
  89. {
  90. area = length * width;
  91. cout << "The area of the rectangle is " << area << ". \n";
  92. }
  93. break;
  94.  
  95. case 3: cout << "Enter the length of the base of the triangle:" << endl;
  96. cin >> base;
  97. if (base < 0)
  98. {
  99. cout << "Base cannot be negative, re-start program and";
  100. cout << " try again.";
  101. }
  102. else
  103. {
  104. cout << "Enter the height of the triangle" << endl;
  105. cin >> height;
  106. }
  107. if (height < 0)
  108. {
  109. cout << "Height cannot be negative, re-start program and";
  110. cout << " try again.";
  111. }
  112. else
  113. { area = (base * height) / 2;
  114. cout << "The area of the triangle is " << area << ". \n";
  115. }
  116. break;
  117.  
  118. case 4: cout << "Program ending. \n";
  119. break;
  120. default: cout << "Please choose from options 1-4, re-run the program";
  121. cout << "and try again." << endl;
  122. break;
  123. }
  124.  
  125. return 0;
  126.  
  127. }
Success #stdin #stdout 0s 5260KB
stdin
1
-1
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 circle's radius:
Radius cannot be negative, re-start program and try again.