fork download
  1. // Cenyao Huang CS1A Homework 4, p. 225, #21
  2.  
  3. /******************************************************************************************************
  4. * CALCULATE GEOMETRY
  5. *
  6. * This program calculates the area of a circle, rectangle, and triangle.
  7. *
  8. * This program uses the following formulas :
  9. * Area of a circle : 2 * 3.14159 * radius
  10. * Area of a rectangle : length * width
  11. * Area of a triangle : 1/2 * base * height
  12. *
  13. * Input
  14. * radius : the value of the radius of the circle
  15. * length : the value of the length of the rectangle
  16. * width : the value of the width of the rectangle
  17. * base : the value of the base of the triangle
  18. * height : the vallue of the height of the triangle
  19. * area: the value of the area of either the circle, rectangle, or triangle
  20. *******************************************************************************************************/
  21.  
  22. #include <iostream>
  23. using namespace std;
  24.  
  25. int main() {
  26. // assign variables
  27. double area;
  28. int num = 0;
  29.  
  30. //display menu
  31. cout << "Geometry Calculator" << endl << "\t 1. Calculate the Area of a Circle" << endl << "\t 2. Calculate the Area of a Rectangle" << endl << "\t 3. Calculate the Area of a Triangle" << endl << "\t 4. Quit" << endl;
  32.  
  33. // if the value entered in the menu is not 4, it will loop
  34. while (num != 4)
  35. {
  36. cin >> num;
  37. if (num > 4 || num < 1)
  38. cout << "Number outside of range.";
  39.  
  40. // get values to calculate area, then display it
  41. switch (num) {
  42. case 1:
  43. double radius;
  44. cout << "Please enter the radius of the circle" << endl;
  45. cin >> radius;
  46. area = 2 * 3.14159 * radius;
  47. cout << "The area of the circle is: " << area << endl;
  48. if (radius < 0)
  49. cout << "Invalid input. Please enter positive numbers.";
  50. break;
  51.  
  52. case 2:
  53. double length, width;
  54. cout << "Please enter the length and width of the rectangle" << endl;
  55. cin >> length >> width;
  56. area = length * width;
  57. cout << "The area of the rectangle is" << area << endl;
  58. if (length < 0 || width < 0)
  59. cout << "Invalid input. Please enter positive numbers.";
  60. break;
  61.  
  62. case 3:
  63. double base, height;
  64. cout << "Please enter the base and height for the triangle" << endl;
  65. cin >> base >> height;
  66. area = base * height;
  67. cout << "The area of the base is: " << area << endl;
  68. if (base < 0 || height < 0)
  69. cout << "Invalid input. Please enter positive numbers.";
  70. break;
  71.  
  72. case 4:
  73. cout << "End of program";
  74. break;
  75. }
  76.  
  77. }
  78. return 0;
  79. }
Success #stdin #stdout 0.01s 5304KB
stdin
1 4 2 5 2 4
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
Please enter the radius of the circle
The area of the circle is: 25.1327
Please enter the length and width of the rectangle: 
The area of the rectangle is: 10
End of program