Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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;
}

Your Program Your program will calculate the occupancy rate for a hotel (the percent of rooms that are occupied). You are given the file occupants.txt which details which rooms in the hotel are occupied. Each row of the file represents a floor in the hotelYou do not know ahead of time how many floors there are in the hotel (in other words, your program must work with an input file of this format that has an arbitrary number of rows). Each room is represented by a single digit. If the value of the digit is 1 the room is occupied, and the digit is 0 if the room is not occupied. Because each floor can have a different number of rooms, the sentinel value -1 is used to indicate that the end of data for that floor has been reached. Use the provided program hotelOcc.epp to get you started. Add it to a project in Visual CH and complete the program. If you use the input file given above, your output should look like the following figure. On CWindowssystem32cmd.exe Floor Occupants 12 11 10 ! 16 10 12 ! 13 A 14 14 The occupancy rate is : 59.9. Press any key to continue . . .

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.