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

In summary: In your code, you have a function called Request that takes two arguments, but you haven't defined the function yet.
  • #1
sheepcountme
80
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
  • #2
As the compiler says you have defined a function Request(int,int) but only declared a Request(int)
 
  • #3
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??
 
  • #5

elevator.cpp(12) : see reference to class template instantiation 'Elevator' being compiled
elevator.cpp(29) : error C2664: 'Elevator::Elevator(int)' : cannot convert parameter 1 from 'int' to 'const Elevator &'
Conversion loses qualifiers
elevator.cpp(29) : error C2065: 'MAXFLOOR' : undeclared identifier

As a scientist, it is important to carefully read and understand the error messages in order to identify and fix the errors in the program. The first error message is indicating that there is no member function named "request" that takes two arguments (int and int) in the Elevator class. This is because in the class declaration, the function only has one parameter (int) and it is missing the second parameter (int MAXFLOOR). This can be fixed by adding the second parameter in the function declaration and definition.

The second error message is pointing to line 29, where the constructor is being called. It is indicating that the argument being passed (int) is not the same type as the parameter (const Elevator&). This can be fixed by changing the parameter type to int in the constructor declaration and definition.

The third error message is indicating that the variable MAXFLOOR is not declared. This is because it is being used as a parameter in the function call but it has not been declared anywhere in the program. To fix this, MAXFLOOR should be declared as a constant variable in the Elevator class or as a constant global variable.

In summary, the errors in the program are related to incorrect function parameters and undeclared variables. By fixing these issues, the program should run without any errors.
 

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

1. What are common errors in elevator programs?

Common errors in elevator programs include faulty sensors, incorrect floor mapping, and programming errors such as missing or incorrect logic statements.

2. How do I troubleshoot elevator program errors?

To troubleshoot elevator program errors, start by checking the sensors and connections for any malfunctions. Then, review the logic statements and programming code for any errors. You can also consult the elevator manual or contact the manufacturer for assistance.

3. What are some safety concerns related to elevator program errors?

Elevator program errors can result in unsafe situations such as the elevator stopping between floors or opening on the wrong floor. This can lead to accidents or injuries for passengers. It is important to regularly maintain and test elevators to prevent these errors.

4. How can I prevent elevator program errors from occurring?

To prevent elevator program errors, it is important to regularly maintain and test elevators. This includes checking sensors, connections, and programming code for any malfunctions or errors. It is also important to follow proper programming protocols and regularly update the elevator software.

5. What should I do if I encounter an error while using an elevator?

If you encounter an error while using an elevator, remain calm and press the emergency button or use the emergency phone to contact the building operator. Do not attempt to fix the error yourself. Wait for trained personnel to address the issue and safely exit the elevator.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
973
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
23
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
13
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
15
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
15
Views
6K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
Back
Top