fork download
  1. #include <iostream> // For input and output stream
  2. using namespace std;
  3.  
  4. /*
  5.  * Function: getLength
  6.  * --------------------
  7.  * Prompts the user to enter the rectangle's length.
  8.  * Returns the entered value as a double.
  9.  */
  10. double getLength() {
  11. double length;
  12. cout << "Enter the length of the rectangle: ";
  13. cin >> length;
  14. return length;
  15. }
  16.  
  17. /*
  18.  * Function: getWidth
  19.  * --------------------
  20.  * Prompts the user to enter the rectangle's width.
  21.  * Returns the entered value as a double.
  22.  */
  23. double getWidth() {
  24. double width;
  25. cout << "Enter the width of the rectangle: ";
  26. cin >> width;
  27. return width;
  28. }
  29.  
  30. /*
  31.  * Function: getArea
  32.  * --------------------
  33.  * Accepts length and width as parameters and calculates the area.
  34.  * Returns the calculated area.
  35.  */
  36. double getArea(double length, double width) {
  37. return length * width;
  38. }
  39.  
  40. /*
  41.  * Function: displayData
  42.  * --------------------
  43.  * Accepts length, width, and area as parameters.
  44.  * Displays the length, width, and area in a formatted message.
  45.  */
  46. void displayData(double length, double width, double area) {
  47. cout << "\n--- Rectangle Data ---" << endl;
  48. cout << "Length: " << length << endl;
  49. cout << "Width: " << width << endl;
  50. cout << "Area: " << area << " square units" << endl;
  51. }
  52.  
  53. /*
  54.  * Function: main
  55.  * --------------------
  56.  * The main driver function.
  57.  * Calls the other functions to get input, compute area, and display the result.
  58.  */
  59. int main() {
  60. // Get rectangle dimensions from the user
  61. double length = getLength(); // Ask for length
  62. double width = getWidth(); // Ask for width
  63.  
  64. // Calculate the area of the rectangle
  65. double area = getArea(length, width);
  66.  
  67. // Display the results
  68. displayData(length, width, area);
  69.  
  70. return 0;
  71. }
  72.  
Success #stdin #stdout 0s 5312KB
stdin
Standard input is empty
stdout
Enter the length of the rectangle: Enter the width of the rectangle: 
--- Rectangle Data ---
Length: 1.15739e-310
Width: 1.15739e-310
Area: 0 square units