What Are Common Mistakes When Programming an Elevator System in C++?

AI Thread Summary
Common mistakes when programming an elevator system in C++ include failing to properly define member functions, as seen in the error related to the `request` function having a different signature than declared. Additionally, ensuring that the `MAXFLOOR` constant is correctly implemented is crucial, as it should be a class constant rather than a local variable. The logic for handling floor requests must also be clear, particularly in distinguishing between moving up and down. Lastly, the program must include a defined entry point to avoid linker errors, which is essential for successful compilation. Proper attention to these details can prevent common pitfalls in C++ programming for elevator systems.
sheepcountme
Messages
80
Reaction score
1

Homework Statement



This program simulates the actions of two elevators in a building. The building has MAXFLOOR floors - make this a program constant and set it to 15.
You need to write an Elevator class. This class will have only one member variable, which is the floor of the building on which the elevator is currently located. You will need a default constructor for the class (one with no arguments) that sets the member variable to 1. You will also need an accessor function for the member variable. Finally, you will need a member function of the class that is invoked whenever someone presses the button to call the elevator to come to some floor x. This function (name it call) should have a single parameter for the requesting floor x, and should do the following:
1. Check to see that the value of x is between 1 and MAXFLOOR. If the floor request is out of range, report that this is an invalid request and do nothing.
2. If x is the current floor, write out "Doors Opening"
3. If x is above the current floor, write out the starting floor, a message that the elevator is going up, and the successive floor numbers until it gets to floor x.
4. If x is below the current floor, do something similar for going down.
The application code should do the following:
1. Create two elevator objects
2. For every elevator call, decide which of the two elevators is closer to the requesting floor and dispatch that elevator.

Homework Equations



c++ stuff

The Attempt at a Solution



#include <iostream>
using namespace std;

class Elevator
{ private:
int currentFloor;
int MAXFLOOR;
public:
Elevator(int = 1);

void request(int);
};

Elevator::Elevator(int cfloor)

{
currentFloor = cfloor;

}

void Elevator::request(int newfloor, int MAXFLOOR)
{

MAXFLOOR=15;

if (newfloor < 1 || newfloor > MAXFLOOR || newfloor == currentFloor)

; // doing nothing

else if (newfloor > currentFloor) // move elevator up

{
cout << "Starting at floor " << currentFloor << endl;

while (newfloor > currentFloor)
{
currentFloor++;
cout << "Going up – now at floor " << currentFloor << endl;
}
cout << "Stopping at floor " << currentFloor << endl;
}

else // move elevator down
{

cout << "Starting at floor " << currentFloor << endl;
while (newfloor < currentFloor)

{

currentFloor--;

cout << "Going down – now at floor " << currentFloor << endl;
}

cout << "Stopping at floor " << currentFloor << endl;

}
return;
}



I am getting the following errors but I don't understand what it says is wrong with my program or how to fix it...
elevator.cpp(23): error C2511: 'void Elevator::request(int,int)' : overloaded member function not found in 'Elevator'
elevator.cpp(5) : see declaration of 'Elevator'
 
Physics news on Phys.org
As the compiler says you have defined a function Request(int,int) but only declared a Request(int)
 
Ah, okay thank you. I'm so awful at comp science.

After fixing that I got just one more error:

LINK : fatal error LNK1561: entry point must be defined

any ideas??
 

Similar threads

Replies
5
Views
2K
Replies
23
Views
3K
Replies
2
Views
2K
Replies
13
Views
2K
Replies
7
Views
2K
Replies
3
Views
2K
Replies
3
Views
1K
Replies
15
Views
7K
Replies
7
Views
7K
Back
Top