//I will be writing a program that calculates the occupancy rate for a hotel. The program will start by asking user how many floors the hotel has. A for loop should then iterate once for each floor. In each iteration, the loop will ask the user for the number of rooms on the floor and how many of them are occupied. After all the iterations, the program will 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 for each individual floor. It will also display the total amounts of rooms, the total amount of rooms occupied, the total amount of rooms unoccupied and the total percentage. Once the program display the necessary information it will ask the user if he/she wants to input a new set of information for another hotel. The problem I have is when you input a new set of information for another hotel it will add the second set of data with the first set of data that was previously inputted. So for example, if the first set of date has a total of 45 rooms, and the second set of data also has a total of 45 rooms, the second set of date will be displayed as having a total of 90 rooms instead of 45 rooms. Therefore, could someone please show me how to correct this problem so the previous data won't be added to the new data. Sorry for the long explanation by the way. Thank you
int Floors;
int *Rooms = nullptr;
int TotalRooms = 0;
do
{
cin >> Floors;
Rooms = newint [Floors];
TotalRooms += 42;
} while ( cond );
Do you need the TotalRooms outside of the loop? No, you don't. Create variables where you need them.
More importantly, what happens with your dynamically allocated arrays?
If you have N hotels, then you allocate N arrays, but you deallocate only the last one of them. That is a memory leak.
1 2 3 4 5 6 7 8 9
do
{
int Floors = 0;
cin >> Floors;
int *Rooms = newint [Floors];
int TotalRooms = 0;
TotalRooms += 42;
delete [] Rooms;
} while ( cond );
PS. It would be easier to use std::vector than manually allocate memory.
It would be "easier" to have array/vector of structs than K separate arrays.
I presume you were forbidden to use them.
Hi, thank you for the help. I have corrected it and everything works fine. yes I agree it would be much more efficient to do it that way. However, I just wanted to see if I could do it with dynamic memory allocation. Thank you again for your help.
#include<iostream>
#include<string>
#include<cstring>
#include<cstdlib>
#include<iomanip>
using namespace std;
int main()
{
string date;
string Hotel;
char Option;
int Floors;
int *Rooms = nullptr;
int *RoomsOccupied = nullptr;
int *RoomsUnOccupied = nullptr;
double *Percentage = nullptr;
do
{
//These integers represent the total:
int TotalRooms = 0;
int TotalRoomsOccupied = 0;
int TotalRoomsUnOccupied = 0;
double TotalPercentage = 0.0;
cout << "Input number of floors in hotel: ";
cin >> Floors;
while (Floors < 1)
{
cout << "Sorry, you may not have less than 1 floor. Please re-enter answer ";
cin >> Floors;
}
Rooms = new int [Floors];
RoomsOccupied = new int [Floors];
RoomsUnOccupied = new int [Floors];
Percentage = new double [Floors];
for (int i = 0; i < Floors; i++)
{
cout << "Input Number of Rooms for Floor #" << i + 1 << " ";
cin >> Rooms[i];
cout << "Input Number of Rooms occupied for Floor #" << i + 1 << " ";
cin >> RoomsOccupied[i];
while (RoomsOccupied[i] > Rooms[i])
{
cout << "The amount of room occupied cannot be greater than the number of room. Please re-input number ";
cin >>RoomsOccupied[i];
}
}
for (int i =0; i<Floors; i++)
{
RoomsUnOccupied[i] = (Rooms[i] - RoomsOccupied[i]);
Percentage[i] = static_cast<double>(RoomsOccupied[i])/static_cast<double>(Rooms[i]);
}
cout << endl;
cout << "This chart below represents the stats for " <<Hotel << " Hotel on " << date << endl << endl;
cout << "Floor: " << setw(5) << "Rooms: " <<setw(6) << "Occupied Rooms: " << setw(6) << "Unoccupied Rooms: " << setw(6) << "Percentage of Occupied Rooms \n";