-4

I have to display result like this on screen.

           Floor         Rooms        Available    Occupied      
           --––––––––––––––––––––––––––––––––––––––––––––––
          Floor 1         23           9             14                           

            ————————————————————————––––––––––––––-----------
            Floor 2       12             5                 7    

how would i do this? This is my code. Please help!!

        int floors, rooms, totalrooms, totaloccupied, occupied, count;
count = 0;

cout << "How many floors does the hotel have?\n";
cin >> floors;

   for (; count <=floors; count++)      {

    cout << "How many rooms on floor " << count << "?\n";
    cin >> rooms;
    cout << "How many of those rooms are currently occupied?\n";
    cin >> occupied;
        if (occupied > rooms)
            {
                cout << "The rooms occupied cannot exceed the number of rooms.\n";
                cout << "How many rooms on floor " << count << " are occupied?\n";
                cin >> occupied;
            }
AgileMani
  • 1
  • 1
  • Possible duplicate of [Format output in a table, C++](https://stackoverflow.com/questions/6755250/format-output-in-a-table-c) – Arkadiusz Tymieniecki Mar 08 '18 at 04:20
  • 2
    You would do this by storing the entered information, for each floor, in an appropriate container, and then once all data is collected iterate through the container in order to format and print the collected data. What exactly about this do you have a specific question about? stackoverflow.com is not a code-writing service. – Sam Varshavchik Mar 08 '18 at 04:20
  • 2
    I think you have some work to do before outputting. As written, you are not storing info for each floor. And your loop is going to run for n+1 floors. And your error checking only checks once. – David Makogon Mar 08 '18 at 04:22
  • 2
    Use `iomanip` to format and your code looks very incomplete. Focus on the logic of getting the available rooms to work properly first, then worry about the presentation of the table. – Callat Mar 08 '18 at 04:22

1 Answers1

-1

Working off of the broken code that you have, here is a baseline implementation.

#include<iostream>
#include<iomanip>
#include<string>
using namespace std;
int main() {
    int floors, rooms, totalrooms, totaloccupied, occupied, availableRooms;
    string dashes = "---------------------------------------------------------------------------";

    cout << "How many floors does the hotel have?\n";
    cin >> floors;
    cout << "Floor " << setw(10) << "Rooms " << setw(20) << "Available " << setw(20) << "Occupied " << endl;
    cout << dashes << endl;
    for (int count = 0; count < floors; count++) {

        cout << "How many rooms on floor " << count << "?\n";
        cin >> rooms;
        cout << "How many of those rooms are currently occupied?\n";
        cin >> occupied;
        if (occupied > rooms)
        {
            cout << "The rooms occupied cannot exceed the number of rooms.\n";
            cout << "How many rooms on floor " << count << " are occupied?\n";
            cin >> occupied;
        }
        availableRooms = rooms - occupied;
        cout << "Floor " << count << setw(10) << rooms << setw(10) << availableRooms << setw(10) << occupied << endl;
        cout << dashes << endl;
    }
    return 0;
}
Callat
  • 2,928
  • 5
  • 30
  • 47
  • @AgileMani did this solve your issue? If so please accept my answer and upvote. If not let me know and I can refine the solution. – Callat Mar 15 '18 at 13:26