fork download
  1. //Angel Lugo CSC5 Chapter 4, P. 225, #21
  2. //
  3. /**************************************************************
  4. *
  5. * COMPUTE THE AREA OF CIRCLE, RECTANGLE, AND TRIANGLE
  6. * ____________________________________________________________
  7. * This program computes area of a circle, rectangle, or triangle
  8. * depending on inputs given for each output.
  9. *
  10. * Computation is based on:
  11. * The user's input on the length, width,
  12. * base, height, and radius asked
  13. * for depending on choosen shape
  14. * ____________________________________________________________
  15. * INPUT
  16. * choice: Number 1-4 picked by user
  17. * radius: Radius entered by user
  18. * length: Length entered by user
  19. * width : Width entered by user
  20. * base : Base entered by user
  21. * height: Height entered by user
  22. *
  23. * OUTPUT
  24. * area: Area of shape picked by user
  25. *
  26. **************************************************************/
  27. #include <iostream>
  28. #include <iomanip>
  29. using namespace std;
  30.  
  31. int main()
  32. {
  33. //
  34. // Declare variables to store inputs
  35. int choice;
  36. float area;
  37. float radius;
  38. float length;
  39. float width;
  40. float base;
  41. float height;
  42. //
  43. // Loop until 4 is enterd
  44. while (true)
  45. {
  46. //
  47. // Display information in cout
  48. cout << "Geometry Calculator\n\n";
  49. cout << setw(37) << "1. Calculate the Area of a Circle\n"
  50. << setw(40) << "2. Calculate the Area of a Rectangle\n"
  51. << setw(39) << "3. Calculate the Area of a Triangle\n"
  52. << setw(12) << "4. Quit\n\n"
  53. << setw(28) << "Enter your choice (1-4): ";
  54. //
  55. // Get and Store User input in "cin" for Variable
  56. cin >> choice;
  57. //
  58. // if the if statement is true do statement
  59. if (choice == 4)
  60. {
  61. //
  62. // Exit loop
  63. break;
  64. }
  65. //
  66. // Process choice inputs and continue with items in case
  67. switch (choice)
  68. {
  69. case 1:
  70. cout << "\nWhat is the radius of the circle? \n";
  71. cin >> radius;
  72. if (radius >= 0)
  73. {
  74. area = 3.14159 * (radius * radius);
  75. cout << "The area of the circle is: " << area << "\n";
  76. }
  77. else
  78. {
  79. cout << "\nDo not enter negative values\n";
  80. }
  81. break;
  82.  
  83. case 2:
  84. cout << "\nWhat is the length of the rectangle? \n";
  85. cin >> length;
  86. cout << "What is the width of the rectangle? \n";
  87. cin >> width;
  88. if (length >= 0 && width >= 0)
  89. {
  90. area = length * width;
  91. cout << "The area of the rectangle is: " << area << "\n";
  92. }
  93. else
  94. {
  95. cout << "\nDo not enter negative values\n";
  96. }
  97. break;
  98.  
  99. case 3:
  100. cout << "\nWhat is the length of the triangle's base? \n";
  101. cin >> base;
  102. cout << "What is the height of the triangle? \n";
  103. cin >> height;
  104. if (base >= 0 && height >= 0)
  105. {
  106. area = base * height * 0.5;
  107. cout << "The area of the triangle is: " << area << "\n";
  108. }
  109. else
  110. {
  111. cout << "\nDo not enter negative values\n";
  112. }
  113. break;
  114. //
  115. // Display cout if invalid number was entered
  116. default:
  117. cout << "\nInvalid Choice. Enter a number between 1 and 4.\n";
  118. }
  119.  
  120. cout << "\nClick Enter to Continue\n";
  121. //
  122. // Ignore other key and wait for Enter key to Continue
  123. cin.ignore();
  124. cin.get();
  125. }
  126. return 0;
  127. }
Success #stdin #stdout 0.01s 5388KB
stdin
1
30

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

   Enter your choice (1-4): 
What is the radius of the circle? 
The area of the circle is: 2827.43

Click Enter to Continue
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):