fork download
  1. // Elaine Torrez Chapter 4 P. 125, #21
  2. /**************************************************************************
  3.  * GEOMETRY CALCULATOR
  4.  * ------------------------------------------------------------------------
  5.  * Displays a menu to calculate the area of a circle, rectangle, or triangle,
  6.  * or quit the program. Prompts for dimensions as needed, then shows the
  7.  * calculated area or an error message for invalid input.
  8.  * ------------------------------------------------------------------------
  9.  * INPUT
  10.  * choice, radius, length, width, base, height
  11.  *
  12.  * OUTPUT
  13.  * Area of the chosen shape, quit message, or error message
  14.  **************************************************************************/
  15.  
  16. #include <iostream>
  17. #include <iomanip>
  18. using namespace std;
  19.  
  20. int main()
  21. {
  22. const double PI = 3.14159; // Constant for circle area formula
  23. int choice; // User's menu choice
  24. double radius; // Radius of circle
  25. double length, width; // Length and width of rectangle
  26. double base, height; // Base and height of triangle
  27. double area; // Calculated area
  28.  
  29. // Display menu
  30. cout << "Geometry Calculator\n"
  31. << "1. Calculate the Area of a Circle\n"
  32. << "2. Calculate the Area of a Rectangle\n"
  33. << "3. Calculate the Area of a Triangle\n"
  34. << "4. Quit\n\n"
  35. << "Enter your choice (1-4): ";
  36. cin >> choice;
  37.  
  38. cout << fixed << setprecision(2);
  39.  
  40. // Process menu choice
  41. switch (choice)
  42. {
  43. case 1: // Circle
  44. cout << "Enter the radius of the circle: ";
  45. cin >> radius;
  46. if (radius < 0)
  47. cout << "Error: Radius cannot be negative.\n";
  48. else
  49. {
  50. area = PI * radius * radius;
  51. cout << "The area of the circle is " << area << endl;
  52. }
  53. break;
  54.  
  55. case 2: // Rectangle
  56. cout << "Enter the length of the rectangle: ";
  57. cin >> length;
  58. cout << "Enter the width of the rectangle: ";
  59. cin >> width;
  60. if (length < 0 || width < 0)
  61. cout << "Error: Length and width cannot be negative.\n";
  62. else
  63. {
  64. area = length * width;
  65. cout << "The area of the rectangle is " << area << endl;
  66. }
  67. break;
  68.  
  69. case 3: // Triangle
  70. cout << "Enter the base of the triangle: ";
  71. cin >> base;
  72. cout << "Enter the height of the triangle: ";
  73. cin >> height;
  74. if (base < 0 || height < 0)
  75. cout << "Error: Base and height cannot be negative.\n";
  76. else
  77. {
  78. area = 0.5 * base * height;
  79. cout << "The area of the triangle is " << area << endl;
  80. }
  81. break;
  82.  
  83. case 4: // Quit
  84. cout << "Program ending.\n";
  85. break;
  86.  
  87. default:
  88. cout << "Error: Please enter a number between 1 and 4.\n";
  89. }
  90.  
  91. return 0;
  92. }
  93.  
Success #stdin #stdout 0.01s 5268KB
stdin
Standard input is empty
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): Error: Please enter a number between 1 and 4.