School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!
Welcome to Dream.In.Code
Become an Expert!

Join 340,138 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 3,864 people online right now. Registration is fast and FREE... Join Now!



Help with an elevator program - Almost done - due wed

Help with an elevator program - Almost done - due wed Rate Topic: -----

#1 carkrazed2005  Icon User is offline

  • New D.I.C Head
  • Pip
  • Group: New Members
  • Posts: 4
  • Joined: 02-December 08


Dream Kudos: 0

Posted 02 December 2008 - 10:45 PM

This should be an easy one for you guys. This is what I have so far. It builds and runs, but I need to put two poeple in it that get on the elevator at different floors. I'm not sure how to do this. I thought about making another class as the person, but I have no idea how to link it. Thanks for any help.

#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;
}


Was This Post Helpful? 1


#2 harshakirans  Icon User is offline

  • D.I.C Head
  • Icon
  • View blog
  • Group: Contributors
  • Posts: 123
  • Joined: 26-April 06


Dream Kudos: 150

Posted 02 December 2008 - 11:30 PM

Hi,

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

Was This Post Helpful? 0
  • +
  • -

#3 carkrazed2005  Icon User is offline

  • New D.I.C Head
  • Pip
  • Group: New Members
  • Posts: 4
  • Joined: 02-December 08


Dream Kudos: 0

Posted 03 December 2008 - 12:07 AM

View Postharshakirans, on 2 Dec, 2008 - 11:30 PM, said:

Hi,

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?
Was This Post Helpful? 0
  • +
  • -

#4 harshakirans  Icon User is offline

  • D.I.C Head
  • Icon
  • View blog
  • Group: Contributors
  • Posts: 123
  • Joined: 26-April 06


Dream Kudos: 150

Posted 03 December 2008 - 01:00 AM

Mmmmm

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.
Was This Post Helpful? 0
  • +
  • -

#5 carkrazed2005  Icon User is offline

  • New D.I.C Head
  • Pip
  • Group: New Members
  • Posts: 4
  • Joined: 02-December 08


Dream Kudos: 0

Posted 03 December 2008 - 01:21 AM

View Postharshakirans, on 3 Dec, 2008 - 01:00 AM, said:

Mmmmm

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
Was This Post Helpful? 0
  • +
  • -

#6 harshakirans  Icon User is offline

  • D.I.C Head
  • Icon
  • View blog
  • Group: Contributors
  • Posts: 123
  • Joined: 26-April 06


Dream Kudos: 150

Posted 03 December 2008 - 01:44 AM

Ok i think i have an idea of what you are looking for..

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

Was This Post Helpful? 0
  • +
  • -

#7 carkrazed2005  Icon User is offline

  • New D.I.C Head
  • Pip
  • Group: New Members
  • Posts: 4
  • Joined: 02-December 08


Dream Kudos: 0

Posted 03 December 2008 - 01:57 AM

View Postharshakirans, on 3 Dec, 2008 - 01:44 AM, said:

Ok i think i have an idea of what you are looking for..

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
Was This Post Helpful? 0
  • +
  • -

#8 ddzi00  Icon User is offline

  • New D.I.C Head
  • Pip
  • Group: New Members
  • Posts: 1
  • Joined: 11-March 09


Dream Kudos: 0

Posted 11 March 2009 - 10:44 AM

hi there. i have a project exactly the same as this one. an elevator program using the concept class. could you please help me with it. maybe do a program that may include a little animation. :)

hope you could help me with this one. i badly need it. I'm having a hard time with classes.

thank.
Was This Post Helpful? 0
  • +
  • -

#9 JackOfAllTrades  Icon User is online

  • Mayor of Simpleton
  • Icon
  • View blog
  • Group: Moderators
  • Posts: 7,128
  • Joined: 23-August 08


Dream Kudos: 50

Expert In: Being annoyed with lazy people.

Posted 11 March 2009 - 10:55 AM

Dream.In.Code has a policy by which we prefer to see a good faith effort on your part before providing source code for homework assignments. Please post the code you have written in an effort to resolve the problem, and our members would be happy to provide some guidance. Be sure to include a description of any errors you are encountering as well.

Post your code like this: :code:

Thanks.
Was This Post Helpful? 0
  • +
  • -



Fast Reply

  

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users



Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month