fork download
  1. //Charlotte Davies-Kiernan CS1A Chapter 4 P.225 #21
  2. //
  3. /******************************************************************************
  4.  *
  5.  * Display a Geometry Calculator
  6.  * ____________________________________________________________________________
  7.  * This program will display a men with 4 options: calculate one of three
  8.  * area calculations, circle, rectangle, and triangle or to quit.
  9.  *
  10.  * The formuals that will be used:
  11.  * area of circle = PI * radius * radius
  12.  * area of rectangle = length * width
  13.  * area of triangle = 0.5 * base * height
  14.  * ____________________________________________________________________________
  15.  * INPUT
  16.  * choice //choice the user picks which decides what the program will output
  17.  * radius //radius of circle the user will enter if they pick 1
  18.  * length //length of rectangle the user will enter if they pick 2
  19.  * width //width of rectangle the user will enter if they pick 2
  20.  * base //base of triangle the user will enter if they pick 3
  21.  * height //height of triangle the user will enter if they pick 3
  22.  *
  23.  * OUTPUT
  24.  * circleArea //Area of circle if the user chooses 1
  25.  * recArea //Area of rectangle if the user chooses 2
  26.  * triArea //Area of triangle if the user chooses 3
  27.  *****************************************************************************/
  28. #include <iostream>
  29. #include <iomanip>
  30. #include <cmath>
  31. using namespace std;
  32. int main()
  33. {
  34. //Variables
  35. double base; //INPUT - base that will need to be entered if user chooses 3
  36. double circleArea; //OUTPUT - area of circle if user chooses 1
  37. double height; //INPUT - height that will need to be entered if user chooses 3
  38. double length; //INPUT - length that will need to be entered if user chooses 2
  39. double radius; //INPUT - radius that will need to be entered if user chooses 1
  40. double recArea; //OUTPUT - area of rectangle if user chooses 2
  41. double triArea; //OUTPUT - area of triangle if user chooses 3
  42. double width; //INPUT - width that will need to be entered if user chooses 2
  43. int choice; //INPUT - the users choice of what the program will output
  44. //
  45. //Display the menu
  46. cout << "Geometry Calculator" << endl;
  47. cout << "1. Calculate the Area of a Circle" << endl;
  48. cout << "2. Calculate the Area of a Rectangle" << endl;
  49. cout << "3. Calcualte the Area of a Triangle" << endl;
  50. cout << "4. Quit" << endl;
  51. cout << "Enter your choice (1-4): "<< endl << endl;
  52. cin >> choice;
  53. //
  54. //Validate input
  55. if (choice < 1 || choice > 4){
  56. cout << "Error: Invalid choice. Please enter a number from 1 to 4" << endl;
  57. return 1;
  58. }
  59. cout << fixed << setprecision(2);
  60. switch (choice) {
  61. case 1: //Circle
  62. cout << "Enter the radius of the circle: ";
  63. cin >> radius;
  64. if (radius < 0)
  65. cout << "Error: Radius cannot be negative" << endl;
  66. else {
  67. circleArea = M_PI * radius * radius;
  68. cout << "The area of the circle is: " << circleArea << endl;
  69. }
  70. break;
  71. case 2: //Rectangle
  72. cout << "Enter the length of the rectangle: " << endl;
  73. cin >> length;
  74. cout << "Enter the width of the rectangle: " << endl;
  75. cin >> width;
  76. if (length < 0 || width < 0)
  77. cout << "Error: length and width cannot be negative." << endl;
  78. else {
  79. recArea = length * width;
  80. cout << "The area of the rectangle is: " << recArea << endl;
  81. }
  82. break;
  83. case 3: //Triangle
  84. cout << "Enter the base of the triangle: " << endl;
  85. cin>> base;
  86. cout << "Enter the height of the triangle: " << endl;
  87. cin >> height;
  88. if (base < 0 || height < 0)
  89. cout << "Error: Base and height cannot be negative." << endl;
  90. else {
  91. triArea = 0.5 * base * height;
  92. cout << "The area of the triangle is: " << triArea << endl;
  93. }
  94. break;
  95. case 4: //Quit
  96. cout << "Exiting the program. Goodbye!" << endl;
  97. break;
  98. }
  99. return 0;
  100. }
Success #stdin #stdout 0.01s 5280KB
stdin
2
45.43
3.75
stdout
Geometry Calculator
1. Calculate the Area of a Circle
2. Calculate the Area of a Rectangle
3. Calcualte 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: 170.36