C++ Hotel Room Help?

Okay so I have this code (I will post underneath as an answer because it is too long)

and it runs perfectly but  when I want the program to end it loops back to asking for the number of rooms again... Why is that? Easy fix i'm assuming. Thoughts?

3 Answers

  • 1 month ago

    You have a misplaced closing brace on the for loop. The } brace just before "return 0;" should be moved up to just before the cout << "-----.,.." line.

    After that, your program should repeat the input for each floor, and only after that will it display the full hotel statistics and then end the program.

    There's also a bug in your 13th floor handling. You should be testing for i==13. I haven't seen what requirements you have for your program, but it's quite legal to have 13 floors (or more) in a hotel. If you use the superstitious "no 13th floor" policy, floors above the 12th floor all have 1 added to their number.

    You'll need to decide if the original "totalFloors" value asked for at the beginning is really a count of the floors in the physical building, or the floor number of the top floor. Those aren't the same number when the hotel has 13 or more physical floors.

    If totalFloors is a physical floor count, then the loop index (i) will be a physical floor number. You can add code at the beginning of the for loop body:

    int currentFloor = i;

    if (currentFloor > 12) currentFloor += 1;

    If totalFloors is actually the floor number of the top floor, then you can change the variable name in the for loop to currentFloor, and then add the following to the top of the loop instead:

    if (currentFloor == 13)


    .... [maybe output a "13th floor skipped" message here]

    .... continue; // skip to next floor without executing the rest of the loop body


    Either way, you'll remove your current test for floor 13 and the loop body will simply ask for the room and booked room counts and add them to the totals. One thing I'd change about that is to include the floor number in the prompt:

    cout << "Enter the number of room on floor " << currentFloor << " that you wish to book: ";

  • EddieJ
    Lv 7
    1 month ago

    The program can't read your mind -- it doesn't know that you want the program to end.

    It seems that you should just delete the <for> loop.

    • EddieJ
      Lv 7
      1 month agoReport

      It seems that it will do it for each floor.

      It seems that maybe you do NOT want that <for> loop.

      It seems that the number of floors is just meant to be used in the final report. So, you just need to remove the <for> loop.... unless I'm misunderstanding the assignment.

  • 1 month ago

    #include <iostream>

    #include <iomanip>

    using namespace std;

    int main()


      int roomsOnFloor = 0;

      float totalRooms = 0.00;

      int totalFloors = 0;

      int occupiedRoomsOnFloor = 0;

      float totalOccupiedRooms = 0.00;

      float percentOccupied = 0.00;

      cout << "Enter the number of floors: ";

      cin >> totalFloors;

      while (totalFloors<1)


        cout << "Number of floors must be greater than 1. Please re enter ";

        cin >> totalFloors;


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


          if (totalFloors!=13)


            cout << "Enter the number of rooms on the floor you wish to book.";

            cin >> roomsOnFloor;




            cout << "Error: 13th floor is skipped. Please re enter. ";

            cin >> totalFloors;

            cout << "Enter the number of rooms on the floor you wish to book. ";

            cin >> roomsOnFloor;


           cout << "How many rooms are occupied? ";

           cin >> occupiedRoomsOnFloor;

         totalRooms += roomsOnFloor;

         totalOccupiedRooms += occupiedRoomsOnFloor;

         percentOccupied = (totalOccupiedRooms / totalRooms * 100);

      cout << "-----------------------------------------------------------------------" << endl;

      cout << "The hotel has a total of " << totalFloors << " floors." << endl << endl;

      cout << "The floor you wish to book has a total of " << totalRooms << " rooms." << endl << endl;

      cout << "There are " << totalOccupiedRooms << " occupied rooms." << endl << endl;

      cout << "There are " << totalRooms - totalOccupiedRooms << " empty rooms." << endl << endl;

      cout << "The percentage of occupied rooms is " << setprecision(2) << percentOccupied << "%" << endl << endl;


      return 0;


Still have questions? Get your answers by asking now.