Ravi Wants to Install an Elevator Program in C++​

Elevator Program in C++

An elevator program simulates the operation of an elevator in a building. The program typically allows for functions such as moving between floors, stopping at a specific floor, and handling user input for desired floors. Below is a detailed implementation of an Elevator Program in C++.

Code: Elevator Program in C++

#include <iostream>
#include <cstdlib>
#include <thread>
#include <chrono>

using namespace std;

// Function to simulate elevator movement
void moveElevator(int currentFloor, int destinationFloor) {
    if (currentFloor < destinationFloor) {
        for (int i = currentFloor + 1; i <= destinationFloor; i++) {
            cout << "Elevator moving up... Floor: " << i << endl;
            this_thread::sleep_for(chrono::seconds(1));
        }
    } else if (currentFloor > destinationFloor) {
        for (int i = currentFloor - 1; i >= destinationFloor; i--) {
            cout << "Elevator moving down... Floor: " << i << endl;
            this_thread::sleep_for(chrono::seconds(1));
        }
    }
    cout << "Elevator has arrived at Floor: " << destinationFloor << endl;
}

int main() {
    int currentFloor = 1; // Starting floor
    int destinationFloor;

    cout << "Welcome to the Elevator System!" << endl;
    cout << "The elevator is currently at Floor: " << currentFloor << endl;

    while (true) {
        cout << "\nEnter the destination floor (1-10) or 0 to exit: ";
        cin >> destinationFloor;

        // Exit condition
        if (destinationFloor == 0) {
            cout << "Exiting the Elevator System. Have a nice day!" << endl;
            break;
        }

        // Validating input
        if (destinationFloor < 1 || destinationFloor > 10) {
            cout << "Invalid floor. Please choose a floor between 1 and 10." << endl;
            continue;
        }

        // Moving the elevator
        moveElevator(currentFloor, destinationFloor);

        // Update the current floor
        currentFloor = destinationFloor;
    }

    return 0;
}

How It Works

  1. Welcome Message and Initialization:
    The program initializes the current floor to 1 and greets the user.
  2. User Input:
    Users are prompted to enter the floor they want to go to. The program checks if the input is valid (between 1 and 10).
  3. Elevator Movement Simulation:
    The moveElevator function simulates the elevator’s movement by iterating through the floors, displaying a message as it “moves,” and pausing briefly using this_thread::sleep_for.
  4. Exit Condition:
    If the user enters, the program exits.

Features

  • Input Validation: Ensures that the user selects a floor within the allowed range.
  • Realistic Movement: Simulates elevator movement with delays for each floor.
  • Reusability: The program uses a loop, allowing multiple operations until the user decides to exit.

Sample Output

Welcome to the Elevator System!
The elevator is currently at Floor: 1

Enter the destination floor (1-10) or 0 to exit: 5
Elevator moving up... Floor: 2
Elevator moving up... Floor: 3
Elevator moving up... Floor: 4
Elevator moving up... Floor: 5
Elevator has arrived at Floor: 5

Enter the destination floor (1-10) or 0 to exit: 3
Elevator moving down... Floor: 4
Elevator moving down... Floor: 3
Elevator has arrived at Floor: 3

Enter the destination floor (1-10) or 0 to exit: 0
Exiting the Elevator System. Have a nice day!

Customization Ideas

  1. Add Floors: Modify the range to include more floors.
  2. Multiple Elevators: Implement logic for managing multiple elevators.
  3. Error Handling: Handle non-integer inputs gracefully.
  4. Voice Announcements: Add text-to-speech libraries for announcing floors.

This program provides a simple and functional simulation of an elevator system, which Ravi can customize further based on specific requirements.

Leave a Comment