c ++循环输出结果表

时间:2018-03-08 04:18:13

标签: c++

我必须在屏幕上显示这样的结果。

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

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

我该怎么做?这是我的代码。请帮忙!!

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

1 个答案:

答案 0 :(得分:-1)

使用您拥有的破碎代码,这是一个基线实现。

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