fork download
  1. //Devin Scheu CS1A Chapter 5, P. 295, #9
  2. //
  3. /**************************************************************
  4. *
  5. * CALCULATE HOTEL OCCUPANCY RATE
  6. * ____________________________________________________________
  7. * This program determines the total number of rooms, occupied
  8. * rooms, unoccupied rooms, and the occupancy percentage for a hotel.
  9. *
  10. * Computation is based on a loop summing rooms and occupied rooms
  11. * across all floors, with percentage calculated as occupied rooms
  12. * divided by total rooms
  13. * ____________________________________________________________
  14. * INPUT
  15. * numberOfFloors : The number of floors in the hotel
  16. * roomsPerFloor : The number of rooms on each floor
  17. * occupiedRooms : The number of occupied rooms on each floor
  18. *
  19. * OUTPUT
  20. * totalRooms : The total number of rooms in the hotel
  21. * totalOccupied : The total number of occupied rooms
  22. * totalUnoccupied : The total number of unoccupied rooms
  23. * occupancyPercentage : The percentage of rooms occupied
  24. *
  25. **************************************************************/
  26.  
  27. #include <iostream>
  28. #include <iomanip>
  29.  
  30. using namespace std;
  31.  
  32. int main () {
  33.  
  34. //Variable Declarations
  35. int numberOfFloors; //INPUT - The number of floors in the hotel
  36. int roomsPerFloor; //INPUT - The number of rooms on each floor
  37. int occupiedRooms; //INPUT - The number of occupied rooms on each floor
  38. int totalRooms; //OUTPUT - The total number of rooms in the hotel
  39. int totalOccupied; //OUTPUT - The total number of occupied rooms
  40. int totalUnoccupied; //OUTPUT - The total number of unoccupied rooms
  41. double occupancyPercentage; //OUTPUT - The percentage of rooms occupied
  42.  
  43. //Prompt for Input
  44. cout << "Enter the number of floors in the hotel: ";
  45. cin >> numberOfFloors;
  46.  
  47. //Input Validation for number of floors
  48. while (numberOfFloors < 1) {
  49. cout << "\nError: Please enter a number greater than or equal to 1: ";
  50. cin >> numberOfFloors;
  51. }
  52.  
  53. //Initialize Variables
  54. totalRooms = 0;
  55. totalOccupied = 0;
  56.  
  57. //Calculate Totals for Each Floor
  58. for (int floor = 1; floor <= numberOfFloors; floor++) {
  59. cout << "\nEnter the number of rooms on floor " << floor << ": ";
  60. cin >> roomsPerFloor;
  61.  
  62. //Input Validation for rooms per floor
  63. while (roomsPerFloor < 0) {
  64. cout << "\nError: Please enter a non-negative number: ";
  65. cin >> roomsPerFloor;
  66. }
  67. cout << "Enter the number of occupied rooms on floor " << floor << ": ";
  68. cin >> occupiedRooms;
  69.  
  70. //Input Validation for number of occupied rooms
  71. while (occupiedRooms < 0 || occupiedRooms > roomsPerFloor) {
  72. cout << "\nError: Please enter a number between 0 and " << roomsPerFloor << ": ";
  73. cin >> occupiedRooms;
  74. }
  75. totalRooms += roomsPerFloor;
  76. totalOccupied += occupiedRooms;
  77. }
  78.  
  79. //Calculate Unoccupied Rooms and Occupancy Percentage
  80. totalUnoccupied = totalRooms - totalOccupied;
  81. occupancyPercentage = (static_cast<double>(totalOccupied) / totalRooms) * 100;
  82.  
  83. //Output Result
  84. cout << "\n";
  85. cout << left << setw(25) << "Total Rooms:" << right << setw(15) << totalRooms << endl;
  86. cout << left << setw(25) << "Occupied Rooms:" << right << setw(15) << totalOccupied << endl;
  87. cout << left << setw(25) << "Unoccupied Rooms:" << right << setw(15) << totalUnoccupied << endl;
  88. cout << left << setw(25) << "Occupancy Percentage:" << right << setw(15) << occupancyPercentage << "%" << endl;
  89.  
  90. return 0;
  91. } //end of main()
Success #stdin #stdout 0.01s 5320KB
stdin
6
4
3
5
5
8
5
2
1
3
3
6
4
stdout
Enter the number of floors in the hotel: 
Enter the number of rooms on floor 1: Enter the number of occupied rooms on floor 1: 
Enter the number of rooms on floor 2: Enter the number of occupied rooms on floor 2: 
Enter the number of rooms on floor 3: Enter the number of occupied rooms on floor 3: 
Enter the number of rooms on floor 4: Enter the number of occupied rooms on floor 4: 
Enter the number of rooms on floor 5: Enter the number of occupied rooms on floor 5: 
Enter the number of rooms on floor 6: Enter the number of occupied rooms on floor 6: 
Total Rooms:                          28
Occupied Rooms:                       21
Unoccupied Rooms:                      7
Occupancy Percentage:                 75%