hello, I just had a quick question on my program. I'm trying to perform an elevator class program to simulate an elevator that has 3 stops. The only problem is that I can't figure out how my floors increase or decrease, it just increases by one.

Here is my code:

#include <iostream>
#include <iomanip>
using namespace std;

class elevator
{
    public:
        elevator();
        void ElevatorMotion();
        int currentFloorNum;
};

int main()
{
    elevator e;
    int ans;

    cout << "Welcome to the Elevator System";
    cout << "\nYou are on the 1st Floor";

    do
    {
        cout << "Would You Like to Go up (press 1)" << endl
             << "or down (press 2) A Floor?" << endl
             << "(-1 to exit after choice input) " << endl;

        cin >> ans;

        if(ans == -1)
           break;

        if(ans == 4)
        cout << "There is no Fourth Floor, Please Choose a Valid Floor" << endl;

        e.ElevatorMotion();

    }while(ans != -1);
}

elevator::elevator()
{
    currentFloorNum = 1;
}

void elevator::ElevatorMotion()
{

    bool goingUp = true;
    bool goingDown = false;
    int currentFloorNum = 1;
    int nextFloor;



        if(goingUp == true)
        {
            nextFloor = currentFloorNum + 1;
            currentFloorNum = currentFloorNum + nextFloor;
            currentFloorNum = nextFloor;
        }

        else
            nextFloor = currentFloorNum - 1;
            currentFloorNum = currentFloorNum + nextFloor;
            currentFloorNum = nextFloor;


    cout << "You are on the " << currentFloorNum << " floor" << endl;
}

should I use a for loop in ElevatorMotion function, I'm trying to differentiate the bool values, when I run my program it just increases by one and stays that way

// How about a relative position change?
bool changeLevel( int velocity )
{ // It may simply your problem
  currentFloorNum += velocity;
  cout << "You are on floor number " << currentFloorNum << "\r\n";
  return velocity;
} // changeLevel(3); changeLevel(-2); changeLevel(1);

Edited 3 Years Ago by Unimportant

How would that manipulate the change? I understand changeLevel(3) would move the position to level three, but how would the user manipulate that?

This article has been dead for over six months. Start a new discussion instead.