虽然循环似乎是从布尔条件反向传递变量

时间:2018-02-15 04:22:30

标签: c++ class while-loop

我已经测试了很多场景,并且没有使用while循环工作;但我似乎无法弄清楚什么搞砸了。如果我在目标范围1-3中选择一个int,它会通过然后冻结,我必须按程序控制该程序。

如果我选择一个超出该范围的数字,它会让它传入while循环并调用该函数。我非常困惑,这是我用课程编写的第一个程序;所以我觉得这可能是个问题。

感谢。

#include <iostream>
using namespace std;

class Elevator
{
    public:
        void floorControl();
        // Outputs floor actions and lets a user pick a floor number
        int status();
        // outputs floor positon
    private:
        int floorPosition = 1;
};

int main()
{
    Elevator building[3];
    int elevatorSelect;
    cout << "\tElevator Status\n\tA\tB\tC\n" 
     << "\t" << building[0].status() << "\t" << building[1].status()
     << "\t" << building[2].status() << endl
     << "\tWhich elevator do you want (1=A, 2=B, 3=C, or other to exit) ? ";
     cin >> elevatorSelect;
     cout << "\t" << elevatorSelect <<  endl;
    while((elevatorSelect <= 3) && (elevatorSelect >=1));
    {
        building[elevatorSelect-1].floorControl();
        cout << "\tElevator Status\n\tA\tB\tC\n" 
             << "\t" << building[0].status() << "\t" << building[1].status()
             << "\t" << building[2].status() << endl
             << "\tWhich elevator do you want (1=A, 2=B, 3=C, or other to exit) ? ";
        cin >> elevatorSelect;
         cout << "\t" << elevatorSelect <<  endl;
    }
    return 0;
}

void Elevator::floorControl()
{
    int floorSelect;
    if(floorPosition > 1)
    {
        cout << "\tStarting at floor " << floorPosition << endl;
        for(int i=1; i>floorPosition; floorPosition--)
            cout << "\t  Going down - now at floor " << floorPosition-1 << endl;
        cout << "\tStopping at floor " << floorPosition << endl;
    }
    cout << "\tWhich floor do you want? ";
    cin >> floorSelect;
    if ((floorSelect < 1 )|| (floorSelect > 10))
        cout << "\t**You pick up some dust off the wall; you missed**\n";
    else 
    {
        cout << "\tStarting at floor " << floorPosition << endl;
        for (floorSelect; floorSelect > floorPosition; floorPosition++)
            cout << "\t  Going up - now at floor " << floorPosition+1 << endl;
        cout << "\tStopping at floor " << floorPosition << endl;
    }
}

int Elevator::status()
{
    return floorPosition;
}

0 个答案:

没有答案