C ++酒店入住和跳过13楼迭代编码项目(不是IT学生)

时间:2016-09-29 10:05:23

标签: c++

对于一个项目,我被要求编写一个计算酒店入住率的程序。该程序应首先询问用户酒店有多少楼层。然后循环应该为每个楼层迭代一次。在每次迭代中,循环应该询问用户地板上的房间数量以及占用的房间数量。在所有迭代之后,程序应该显示酒店有多少房间,有多少房间被占用,有多少房间空置,以及房间占用的百分比。百分比可以通过将房间数除以房间数来计算。

注意:传统上大多数酒店都没有十三层。该程序中的循环应该跳过整个第十三次迭代。

我现在的问题是该程序仍然要求占用的房间数量,虽然它确实跳过询问13楼的房间数量。

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<iomanip.h>

int main()
{
    clrscr();
    int roomsOnFloor = 0;
    float totalRooms = 0.00;
    int totalFloors = 0;
    int occupiedRoomsOnFloor = 0;
    float totalOccupiedRooms = 0.00;
    float percentOccupied = 0.00;

    cout << "Enter number of floors: ";
    cin >> totalFloors;

    while (totalFloors < 1)
    {
        cout << "Number of floors must be at least 1. Please re-enter ";
        cin >> totalFloors;
    }

    for (int i = 1; i <= totalFloors; i++)
    {
        if (i != 13)
        {
            cout << "Enter the number of rooms on the floor ";
            cin >> roomsOnFloor;

            while (roomsOnFloor < 10)
            {
                cout << "Number of rooms on floor must be at least 10. Please re-enter ";
                cin >> roomsOnFloor;
            }
        }
        cout << "How many rooms are occupied? ";
        cin >> occupiedRoomsOnFloor;

        totalRooms += roomsOnFloor;
        totalOccupiedRooms += occupiedRoomsOnFloor;
    }
    percentOccupied = (totalOccupiedRooms / totalRooms)*100;

    clrscr();
    cout << "The hotel has total of " << totalFloors << " floors\n";
    cout << "The hotel has total of " << totalRooms << " rooms\n";
    cout << "There are " << totalOccupiedRooms << " rooms occupied\n";
    cout << "There are " << totalRooms - totalOccupiedRooms << " empty rooms\n";
    cout << "Percentage of occupied rooms is " << setprecision(2) << percentOccupied << "%\n";

    getch();
    return 0;
}

4 个答案:

答案 0 :(得分:2)

这是正确的方法。

如果您在['cat', 'window', 'blahblah'] ^ ['cat', 'window', 'blahblah'] ^ ['cat', 'window', 'blahblah'] ^ ['blahblah', 'cat', 'window', 'blahblah'] ^ ['blahblah', 'blahblah', 'cat', 'window', 'blahblah'] ^ ... 条件之外使用它,那么它将无法计算13层楼的占用房间数量。您需要将其置于if条件下,然后才能计算if

numberofoccupiedspace

答案 1 :(得分:1)

当然它会询问13楼的房间数量,因为这样做的说明不在if,所以每次都要执行,即使楼层是13.你必须把if

中的所有内容
if (i != 13)
{

    cout << "Enter the number of rooms on the floor ";
    cin >> roomsOnFloor;

    while (roomsOnFloor < 10)
    {

        cout << "Number of rooms on floor must be at least 10. Please re-enter ";
        cin >> roomsOnFloor;
    }

    cout << "How many rooms are occupied? ";
    cin >> occupiedRoomsOnFloor;

    totalRooms += roomsOnFloor;
    totalOccupiedRooms += occupiedRoomsOnFloor;
}

答案 2 :(得分:1)

您要求if以外的被占用房间数:

if (i != 13)
{
    cout << "Enter the number of rooms on the floor ";
    cin >> roomsOnFloor;

    //...
}

cout << "How many rooms are occupied? ";
cin >> occupiedRoomsOnFloor;

很明显,你只是要跳过询问房间的数量,而不是占用房间的数量。

BTW:这是学习如何使用调试器的一个好点。单步执行这个程序就可以做到这一点。

答案 3 :(得分:1)

Because you ask for the number of occupied rooms outside the if block, you should change the code to:

    if (i != 13)
    {

        cout << "Enter the number of rooms on the floor ";
        cin >> roomsOnFloor;

        while (roomsOnFloor < 10)
        {

        cout << "Number of rooms on floor must be at least 10. Please re-enter ";
        cin >> roomsOnFloor;


        }

    cout << "How many rooms are occupied? ";
    cin >> occupiedRoomsOnFloor;
    }