In: Computer Science
( C++ ) Occupancy rate is often considered to be one of the top three most useful metrics for hotel owners. Generally speaking, those working in the hotel industry should be aiming for a high occupancy rate, because this indicates that space is being used efficiently.
The occupancy rate of a hotel is expressed as a percentage. So, for example, if a hotel has 100 rooms available to be sold and 60 of those rooms are occupied, the occupancy rate would be 60 percent.
How to Calculate Occupancy Rate
The occupancy rate can be calculated with the following formula:
Occupancy Rate = Number of Occupied Rooms / Total Number of Available Rooms
Example: If your hotel has 220 rooms and 210 of the rooms are occupied:
210 / 220 = 0.95 = 95 percent occupancy rate.
Program Description
Write a program that calculates the occupancy rate for a hotel. The program should start by asking the user how many floors the hotel has. Use the following message for the prompt: "How many floors does the hotel have? ".
A loop should then iterate once for each floor. In each iteration, the loop should ask the user for the number of rooms on the floor and how many are occupied. Use the following messages for the prompts:
"\nEnter the number of rooms on floor <floor number>: " and "\nHow many rooms are occupied on floor <floor number>? ".
Note: <floor number> should be substituted whith the floor number.
After all the iterations complete, the program should display a report similar to the following:
Total number of rooms: 330
Occupied rooms: 264
Unoccupied rooms: 66
Occupancy Rate: 0.80
Note: A new line should be displayed before the report is displayed and the occupancy rate should be displayed with 2 digits after the decimal.
Hint: Your program should use 2 accumulators to keep track of the total number of rooms in the hotel and how many rooms are occupied. Don't forget to initialize these variables to zero.
Input Validation
Loops should be used to continue prompting the user for input, until it is valid. If invalid input is entered, an error message should be displayed and the input should be read again. Use the error messages below.
Error messages:
Note: <max rooms> should be replaced with the maximum number of rooms on the respective floor.
Reminder
Don't forget that to avoid integer division, one of the operands should be a floating point number. One way to avoid this is to use the static_cast operator to convert one of the operands to a double before dividing. Keep this in mind when you calculate the occupancy rate.
//The code will be
#include <iostream>
using namespace std;
int main() {
int noFloors, totalRooms = 0, totalOccupiedRooms = 0,
rooms, occupiedRooms, i;
cout<<"How many floors does the hotel have?
";
cin>>noFloors;
while(noFloors < 1) {
cout<<"\n->Number of
floors must be 1 or greater. Try again: ";
cin>>noFloors;
}
for(i=1;i<=noFloors;i++){
cout<<"\nEnter the number of
rooms on floor "<<i<<": ";
cin>>rooms;
while(rooms<10){
cout<<"\n->Number of rooms must be 10 or greater. Try
again: ";
cin>>rooms;
}
cout<<"How many rooms are
occupied on floor "<<i<<"? ";
cin>>occupiedRooms;
while(occupiedRooms<0 ||
occupiedRooms>rooms)
{
cout<<"\n->Number of occupied rooms must be between 0 and
"<<rooms<<". Try again: ";
cin>>occupiedRooms;
}
totalRooms += rooms;
totalOccupiedRooms +=
occupiedRooms;
}
double occupancyRate =
(double)totalOccupiedRooms/(double)totalRooms;
//Output
cout<<"\nTotal number of rooms:
"<<totalRooms;
cout<<"\nOccupied rooms:
"<<totalOccupiedRooms;
cout<<"\nUnoccupied rooms:
"<<(totalRooms-totalOccupiedRooms);
printf("\nOccupancy Rate: %.2f",occupancyRate);
return 0;
}
//The output will be