Harri Caceres - 1 year ago 255
C++ Question

# C++ hotel occupancy and skipping 13th floor iteration coding project (not an IT student)

For a project, I was asked to 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. 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 of them are occupied. After all the iterations, the program should display how many rooms the hotel has, how many of them are occupied, how many are unoccupied, and the percentage of rooms that are occupied. The percentage may be calculated by dividing the number of rooms occupied by the number of rooms.

Note: It is traditional that most hotels do not have a thirteenth floor. The loop in this program should skip the entire thirteenth iteration.

My problem now is the program still asks for the number of occupied rooms although it does skip asking the number of room on the 13th floor.

``````#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<iomanip.h>

int main()
{
clrscr();
int roomsOnFloor = 0;
float totalRooms = 0.00;
int totalFloors = 0;
int occupiedRoomsOnFloor = 0;
float totalOccupiedRooms = 0.00;
float percentOccupied = 0.00;

cout << "Enter number of floors: ";
cin >> totalFloors;

while (totalFloors < 1)
{
cout << "Number of floors must be at least 1. Please re-enter ";
cin >> totalFloors;
}

for (int i = 1; i <= totalFloors; i++)
{
if (i != 13)
{
cout << "Enter the number of rooms on the floor ";
cin >> roomsOnFloor;

while (roomsOnFloor < 10)
{
cout << "Number of rooms on floor must be at least 10. Please re-enter ";
cin >> roomsOnFloor;
}
}
cout << "How many rooms are occupied? ";
cin >> occupiedRoomsOnFloor;

totalRooms += roomsOnFloor;
totalOccupiedRooms += occupiedRoomsOnFloor;
}
percentOccupied = (totalOccupiedRooms / totalRooms)*100;

clrscr();
cout << "The hotel has total of " << totalFloors << " floors\n";
cout << "The hotel has total of " << totalRooms << " rooms\n";
cout << "There are " << totalOccupiedRooms << " rooms occupied\n";
cout << "There are " << totalRooms - totalOccupiedRooms << " empty rooms\n";
cout << "Percentage of occupied rooms is " << setprecision(2) << percentOccupied << "%\n";

getch();
return 0;
}
``````

This is right way to do it.

If you are using it out of the `if` condition then it will not calculate the number of occupied rooms on 13 floors. You need put that in the `if` condition then it will be able to calculate `numberofoccupiedspace`:

``````                #include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<iomanip.h>

int main()
{
clrscr();
int roomsOnFloor = 0;
float totalRooms = 0.00;
int totalFloors = 0;
int occupiedRoomsOnFloor = 0;
float totalOccupiedRooms = 0.00;
float percentOccupied = 0.00;

cout << "Enter number of floors: ";
cin >> totalFloors;

while (totalFloors < 1)
{
cout << "Number of floors must be at least 1. Please re-enter ";
cin >> totalFloors;
}

for (int i = 1; i <= totalFloors; i++)
{

if (i != 13)
{

cout << "Enter the number of rooms on the floor ";
cin >> roomsOnFloor;

while (roomsOnFloor < 10)
{

cout << "Number of rooms on floor must be at least 10. Please re-enter ";
cin >> roomsOnFloor;
}

cout << "How many rooms are occupied? ";
cin >> occupiedRoomsOnFloor;
totalRooms += roomsOnFloor;
totalOccupiedRooms += occupiedRoomsOnFloor;
}
}

percentOccupied = (totalOccupiedRooms / totalRooms)*100;

clrscr();

cout << "The hotel has total of " << totalFloors << " floors\n";
cout << "The hotel has total of " << totalRooms << " rooms\n";
cout << "There are " << totalOccupiedRooms << " rooms occupied\n";
cout << "There are " << totalRooms - totalOccupiedRooms << " empty rooms\n";
cout << "Percentage of occupied rooms is " << setprecision(2) << percentOccupied << "%\n";

getch();

return 0;
}
``````
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download