fork download
  1. // Elaine Torrez CS1A Chapter 6, P.369, #2
  2. // *****************************************************************************************
  3. // * RECTANGLE AREA *
  4. // *--------------------------------------------------------------------------------------- *
  5. // * This program asks the user to enter the length and width of a rectangle, then *
  6. // * calculates and displays the rectangle’s area. *
  7. // * The program uses separate functions to get the input values, calculate the area, *
  8. // * and display the results. *
  9. // *--------------------------------------------------------------------------------------- *
  10. // * INPUT *
  11. // * length : Length of the rectangle (must be positive) *
  12. // * width : Width of the rectangle (must be positive) *
  13. // *--------------------------------------------------------------------------------------- *
  14. // * OUTPUT *
  15. // * area : The calculated area of the rectangle *
  16. // *****************************************************************************************
  17.  
  18. #include <iostream>
  19. #include <iomanip>
  20. #include <limits> // for input validation
  21. using namespace std;
  22.  
  23. // ---------------- Function Prototypes ----------------
  24. double getLength();
  25. double getWidth();
  26. double getArea(double length, double width);
  27. void displayData(double length, double width, double area);
  28.  
  29. // ---------------------- MAIN -------------------------
  30. int main()
  31. {
  32. double length, width, area;
  33.  
  34. // Get length and width using input functions
  35. length = getLength();
  36. width = getWidth();
  37.  
  38. // Calculate the area
  39. area = getArea(length, width);
  40.  
  41. // Display results
  42. displayData(length, width, area);
  43.  
  44. return 0;
  45. }
  46.  
  47. // ---------------- Function Definitions ----------------
  48.  
  49. // Function to get the rectangle's length
  50. double getLength()
  51. {
  52. double length;
  53. cout << "Enter the rectangle's length: ";
  54. cin >> length;
  55.  
  56. // Validate input
  57. while (cin.fail() || length <= 0.0)
  58. {
  59. cin.clear();
  60. cin.ignore(numeric_limits<streamsize>::max(), '\n');
  61. cout << "ERROR: Length must be positive. Re-enter: ";
  62. cin >> length;
  63. }
  64.  
  65. return length;
  66. }
  67.  
  68. // Function to get the rectangle's width
  69. double getWidth()
  70. {
  71. double width;
  72. cout << "Enter the rectangle's width: ";
  73. cin >> width;
  74.  
  75. // Validate input
  76. while (cin.fail() || width <= 0.0)
  77. {
  78. cin.clear();
  79. cin.ignore(numeric_limits<streamsize>::max(), '\n');
  80. cout << "ERROR: Width must be positive. Re-enter: ";
  81. cin >> width;
  82. }
  83.  
  84. return width;
  85. }
  86.  
  87. // Function to calculate the rectangle's area
  88. double getArea(double length, double width)
  89. {
  90. return length * width;
  91. }
  92.  
  93. // Function to display the results
  94. void displayData(double length, double width, double area)
  95. {
  96. cout << fixed << setprecision(2);
  97. cout << "\nLength : " << length << endl;
  98. cout << "Width : " << width << endl;
  99. cout << "Area : " << area << endl;
  100. }
  101.  
Success #stdin #stdout 0.01s 5292KB
stdin
5
3
stdout
Enter the rectangle's length: Enter the rectangle's width: 
Length : 5.00
Width  : 3.00
Area   : 15.00