fork download
  1. //Maxwell Brewer CS1A Chapter 4, P. 225, #21
  2. //
  3. /***************************************************************
  4.  *
  5.  * CALCULATE AREA OF CIRCLE, RECTANGLE, TRIANGLE
  6.  * ________________________________________________
  7.  *
  8.  * This program will take user inout of a selection
  9.  * to calculate either the area of a circle, rectandgle,
  10.  * or triangle, then the user will input the measurements.
  11.  * _____________________________________________
  12.  * INPUT
  13.  *
  14.  * user choice
  15.  * length, width
  16.  * base, height
  17.  * length, height
  18.  *
  19.  * OUTPUT
  20.  *
  21.  * User menu
  22.  * Circle sqare units
  23.  * Rectangle sqare units
  24.  * Triangle sqare units
  25.  *
  26.  ***************************************************************/
  27.  
  28. #include <iostream>
  29. #include <iomanip>
  30. #include <string>
  31. #include <cmath>
  32.  
  33. using namespace std;
  34.  
  35. int main() {
  36.  
  37. // Initialization
  38. const char MSG_1[] = "Geometry Calculator";
  39. const char SEL_1[] = " 1. Calculate the Area of a Circle";
  40. const char SEL_2[] = " 2. Calculate the Area of a Rectangle";
  41. const char SEL_3[] = " 3. Calculate the Area of a Triangle";
  42. const char SEL_4[] = " 4. Quit";
  43. const char ENT_1[] = " Enter your choice (1-4):";
  44.  
  45. int choice; //declare a variable to store user choice.
  46. //Other init. will come later
  47.  
  48. //Menu
  49. cout << MSG_1 << endl;
  50. cout << SEL_1 << endl;
  51. cout << SEL_2 << endl;
  52. cout << SEL_3 << endl;
  53. cout << SEL_4 << endl
  54. << endl;
  55. cout << ENT_1 << endl;
  56.  
  57. // Display input prompts
  58. cin >> choice;
  59.  
  60. bool valid; //declare a boolean variable to validate input
  61.  
  62. cout << setprecision(4) << fixed << showpoint;
  63.  
  64. // Output
  65. if(choice == 1){
  66. const double PI = 3.14159; //declare and initialize pi
  67. double radius; //declare variable to store radius
  68.  
  69. cout << "Enter radius: " << endl; //prompt to enter radius
  70. cin >> radius; //read radius from keyboard as user input
  71. valid = (radius > 0); //valid will be true if input is positive number
  72.  
  73. if(valid){ //execute this only if input is valid
  74. //calculate and print area
  75. cout << "The area of the Circle is: ";
  76. cout << (pow(radius, 2) * PI) << " sq. units." <<endl;
  77. }
  78. else{ //execute this if input is invalid
  79. cout << "Invalid input!" << endl;
  80. }
  81. }
  82. else if(choice == 2){
  83. //declare variables to store length and width
  84. double length;
  85. double width;
  86.  
  87. //prompt to enter length and width
  88. cout << "Enter length and width: " << endl;
  89.  
  90. //read them from keyboard
  91. cin >> length >> width;
  92.  
  93. //boolean variable valid will be true if
  94. //both length and width are positive
  95. valid = (length > 0) && (width > 0);
  96.  
  97. if(valid){ //execute only if input is valid
  98. //calculate and print area
  99. cout << "The area of the Rectangle is: ";
  100. cout << (length * width) << " sq. units." << endl;
  101. }
  102. else{ //execute this if input is invalid
  103. cout << "Invalid input!" << endl;
  104. }
  105. }
  106. else if(choice == 3){
  107. //declare variables to store base and height
  108. double base;
  109. double height;
  110.  
  111. //prompt to enter base and height
  112. cout << "Enter base and height: " << endl;
  113.  
  114. //read them from keyboard
  115. cin >> base >> height;
  116.  
  117. //boolean variable valid will be true if
  118. //both base and height are positive
  119. valid = (base > 0) && (height > 0);
  120.  
  121. if(valid){ //execute only if input is valid
  122. //calculate and display area
  123. cout << "The area of the Triangle is: ";
  124. cout << base * height/2 << " sq. units." << endl;
  125. }
  126. else{ //execute this if input is invalid
  127. cout << "Invalid input!" << endl;
  128. }
  129. }
  130. else if(choice >= 5){
  131. cout << "ERROR: Choice out of range!" << endl;
  132. }
  133.  
  134.  
  135. return 0;
  136. }
Success #stdin #stdout 0s 5284KB
stdin
3
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

    Enter your choice (1-4):
Enter base and height: 
The area of the Rectangle is: 4.0000 sq. units.