Hotel Occupancy question on nested loops please. Cannot use any other variables
ID: 3600056 • Letter: H
Question
Hotel Occupancy question on nested loops please. Cannot use any other variables other than the ones specified. The following is my work and it gives me the right number of rooms and the correct percentage, but the Occupancy for each floor is not coming out correct. Any advice would be appreciated. Thank you in advance!
int main()
{
// YOU MUST USE THESE VARIABLES ONLY
// YOU MAY NOT DECLARE ANY ADDITIONAL VARIABLES
ifstream inFile; // the input file
int room; // 1 if the room is occupied, 0 if not, -1 if end of floor
int numOcc; // total number of occupied rooms
int floorOcc; // number of occupied rooms on a floor
int numRooms; // total number of rooms
int floorNum; // floor number
double occRate; // occupancy rate
///////////////////////////////
// Your code starts here
// Open the input file
inFile.open("occupants.txt");
// Check if there was an error opening the input file
// Exit the program if there is an error
if (!inFile)
{
cout << "Error opening input file" << endl;
return -1;
}
// Initialize the counter variables, and any other variables
// that need to be initialized.
numOcc = 0;
numRooms = 0;
floorNum = 0;
occRate = 0;
floorOcc = 0;
// Do any other work that needs to be done before the loop starts
cout << " "<< "Floor | Occupants" << endl;
cout << "----------------------" << endl;
// Loop through the input file.
// Your loop must be able to handle an input file of any length.
// In other words, you do not know how many floors the hotel has.
// For each row in the input file, count the number of rooms
// and the number of those rooms that are occupied.
// You do not know how many rooms are on each floor.
// The end of a floor is indicated by the sentinal value -1.
// Be careful to not include the sentinal in your count!
// Print the floor number and number of occupied rooms on that
// floor in a nicely formatted table.
while (!inFile.eof())
{
inFile >> room;
if (room == 1)
{
numOcc++;
floorOcc = 0;
floorOcc = (floorOcc + numOcc) - 1;
numRooms++;
floorOcc++;
}
else if (room == 0)
{
numRooms++;
}
else if (room == -1)
{
floorNum++;
cout << setw(10) << floorNum << " |" << setw(10) << floorOcc << endl;
// Print the floor number and number of occupied rooms on that
// floor in a nicely formatted table.
}
//while (inFile.eof()) {
// cout << setw(10) << floorNum << " |" << setw(10) << floorOcc << endl;
// break;
//}
// End of looping through the input file
}
// End of looping through the input file
// Calculate the occupancy rate.
// Watch out for integer division!
// numOcc = 161;
// numRooms = 269;
// cout << "TOTAL OCCUPANTS: " << numOcc << endl << endl;
occRate = static_cast <double> ((numOcc * 100))/ numRooms ;
// Print out the result as a percentage with exactly one
// decimal point.
cout << endl;
cout << "The occupancy rate is: " << fixed << setprecision(1) << occRate << "%" << endl << endl;
// Close the input file
inFile.close();
// End of your code
///////////////////////////////
system("pause");
return 0;
}
Explanation / Answer
Solution:
Since you have note provided the input data set, or format of input so this could not be testified, but what could be found was floorOcc is being initialized twice, once before the while loop where reading the file starts, and the other is inside the first if-condition.
// Initialize the counter variables, and any other variables
// that need to be initialized.
numOcc = 0;
numRooms = 0;
floorNum = 0;
occRate = 0;
floorOcc = 0; //FIRST TIME
// Do any other work that needs to be done before the loop starts
cout << " "<< "Floor | Occupants" << endl;
cout << "----------------------" << endl;
// Loop through the input file.
// Your loop must be able to handle an input file of any length.
// In other words, you do not know how many floors the hotel has.
// For each row in the input file, count the number of rooms
// and the number of those rooms that are occupied.
// You do not know how many rooms are on each floor.
// The end of a floor is indicated by the sentinal value -1.
// Be careful to not include the sentinal in your count!
// Print the floor number and number of occupied rooms on that
// floor in a nicely formatted table.
while (!inFile.eof())
{
inFile >> room;
if (room == 1)
{
numOcc++;
floorOcc = 0; //SECOND TIME
floorOcc = (floorOcc + numOcc) - 1;
numRooms++;
floorOcc++;
}
initializing it outside the if condition just after inFile >> room; might do the trick.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.