#include <iostream>
#include <stdlib.h>
#include <algorithm>
using namespace std;
const int MAXFLOOR = 50;
// class declaration
class Elevator
{
private:
int cur_floor;
public:
Elevator(int = 1); // constructor
void request(int);
};
// implementation section
Elevator::Elevator(int cfloor)
{
cur_floor = cfloor;
}
void Elevator::request(int newfloor)
{
if (newfloor < 1 || newfloor > MAXFLOOR || newfloor == cur_floor)
; // do nothing
else if ( newfloor > cur_floor) // move elevator up
{
cout << "\nStarting at floor " << cur_floor << endl;
while (newfloor > cur_floor)
{
cur_floor++; // add one to current floor
cout << " Going Up - now at floor " << cur_floor << endl;
}
cout << "Stopping at floor " << cur_floor << endl;
}
else // move elevator down
{
cout << "\nStarting at floor " << cur_floor << endl;
while (newfloor < cur_floor)
{
cur_floor--; // subtract one from current floor
cout << " Going Down - now at floor " << cur_floor << endl;
}
cout << "Stopping at floor " << cur_floor << endl;
}
return;
}
int main()
{
Elevator a, b; // declare 1 object of type Elevator
a.request(6);
a.request(3);
b.request(15);
b.request(26);
system("pause");
return 0;
}
Help with an elevator program - Almost done - due wed
Help with an elevator program - Almost done - due wed
#1
Posted 02 December 2008 - 10:45 PM
#2
Posted 02 December 2008 - 11:30 PM
As to what i understood from your question, I feel that you want to emulate a real scenario where the current position of the elevator will be shared by the persons interacting with the elevator and display the elevator movements accordingly.---------> Correct me here if i got your question wrong ??
Looking at the code
Well you have declared the cur_floor as private int, which servers as an instance variable, and thus is not shared among two objects a and b.
To make your program remember the cur_floor while interacting with other objects it should be declared static
Static int cur_floor
and static variables initialization is as follows
returntype classname::variable=value
int Elevator::cur_floor=1; //before instantiating the class variables.
so you have to remove/modify the constructor.
Try it..
Hope this helps .....
This post has been edited by harshakirans: 02 December 2008 - 11:41 PM
#3
Posted 03 December 2008 - 12:07 AM
harshakirans, on 2 Dec, 2008 - 11:30 PM, said:
As to what i understood from your question, I feel that you want to emulate a real scenario where the current position of the elevator will be shared by the persons interacting with the elevator and display the elevator movements accordingly.---------> Correct me here if i got your question wrong ??
Looking at the code
Well you have declared the cur_floor as private int, which servers as an instance variable, and thus is not shared among two objects a and b.
To make your program remember the cur_floor while interacting with other objects it should be declared static
Static int cur_floor
and static variables initialization is as follows
returntype classname::variable=value
int Elevator::cur_floor=1; //before instantiating the class variables.
so you have to remove/modify the constructor.
Try it..
Hope this helps .....
Thanks for the reply.
I need there to be 2 people waiting on 2 different floors, then elevators will pick them up and drop them of at they're desired floors.
I'm confused about what you suggested.
Do you think I should create a class as the Person then use a sort function for the elevator to pick them up?
#5
Posted 03 December 2008 - 01:21 AM
harshakirans, on 3 Dec, 2008 - 01:00 AM, said:
Can you elaborate more on your requirement and post the exact user input and required output....Along with the code you were working on so far.
This is what I would like it to output.
Person a on floor 4
Person b on floor 5
Starting on floor 1
going up - now on floor 2
going up - now on floor 3
going up - now on floor 4
Person a onboard
Person a requests floor 6
going up - now on floor 5
Person b onboard
person b requests floor 7
going up - now on floor 6
Person a sucssessfully transported
going up - now on floor 7
Person b successfully transported
I have not made any progress since posting previous code. I have tried several things but they were all dead ends.
Thank you for your help. It is much appreciated
#6
Posted 03 December 2008 - 01:44 AM
first thing you need to make the cur_floor variable static as i had described in the post before this.
secondly from the expected output posted by you i feel
u were tracking the person (a,b\), so you need to pass a person parameter
Also u were tracking the pickup and drop of each person, so pick/drop parameter needs to be passed
To the request function.
This solves the first part.
And then you have to sort the requests based on the pickup/drop priority based on the floor.
This post has been edited by harshakirans: 03 December 2008 - 01:44 AM
#7
Posted 03 December 2008 - 01:57 AM
harshakirans, on 3 Dec, 2008 - 01:44 AM, said:
first thing you need to make the cur_floor variable static as i had described in the post before this.
secondly from the expected output posted by you i feel
u were tracking the person (a,b\), so you need to pass a person parameter
Also u were tracking the pickup and drop of each person, so pick/drop parameter needs to be passed
To the request function.
This solves the first part.
And then you have to sort the requests based on the pickup/drop priority based on the floor.
Ok, that gives me somewhere to get started. Now its just figuring out how to do it, lol. Thanks for the help
#8
Posted 11 March 2009 - 10:44 AM
hope you could help me with this one. i badly need it. I'm having a hard time with classes.
thank.
#9
Posted 11 March 2009 - 10:55 AM
Post your code like this:
Thanks.

Start a new topic
Add Reply


MultiQuote



| 


