🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: comp.sys.mac.programmer.codewarrior
1 messages
1 total messages Started by Arthur Cinader Sat, 09 Dec 1995 19:17
Elevator puzzle and Segmentation fault!
#15
Author: Arthur Cinader
Date: Sat, 09 Dec 1995 19:17
552 lines
11081 bytes
I am trying to teach myself to program using c++.

I am working on an excercise to build an elevator simulator.
I have written all of the source for nine classes plus the
main driver.

Everything compiles, but it doesn't run correctly.  I have
located the exact line that is causing the segmentation fault,
but I do not know what the problem is.

I am also unsure of how best to ask this forum for help.  I am
banging my head against a wall so I hope someone can point out
the errors of my way.

The classes are:

The "interactive classes"
Elevator
Person
Floor
Button
Scheduler

The "passive classes"
Bell
Light
Door
Building

The basic scenario:


A building with two floors and an elevator.  The floors and
the elevator can only hold one person.  

The main has a loop that starts at 0 and runs to 99.  Each
pass of the loop checks the scheduler for each floor to so if
a person will arrive this pass (people are scheduled to arrive
every 5 to 25 seconds).  When the peron is created, he is
placed on a floor and then presses the floors call button
(which works fine!). The elevator goes to that floor (if it
needs to) and arrives by ringing the bell, opening the door.
The peron boards the elevator (which works fine) and then
decides which button to press (either button1 or button2).
When the person pushes the button, the simulator crashes. 


Here is the output:

% ./simulator.out
Good Morning and Welcome to arcin.net's Corporate office.
The current time is: 0
0
1
2
3
4
5
6
7
8
9
10
11
Person 1 Just arrived on floor 1
Button on
Turning Light on
Elevator door open
Button off
Button off
bug check 1
person number 1 boards the elevator
person number 1 presses the botton for floor 2
bug check b
bug check d
zsh: segmentation fault (core dumped)  ./simulator.out
%


The relevant classes (I bleieve) are Elevator, Button, and
Person.  I have included the header and source files for those
three below.  Please note that I have added some output lines
that say "bug check #" to help me identify the exact line that
is causing mischif.  The line in question is in the person.c++
function boardElevator().  When the peron object goes to press
the button for the destination floor with the code:

origin.elevator.button2.pushButton();

is when the crash occurs.

I have NO OTHER resource for assitance, so I hope you can
help.  I will obviously supply any additional info needed.

Best Regards,

Arthur Cinader Jr.
acinader@panix.com

***********

// person.h

#ifndef PERSON_H
#define PERSON_H

#include "floor.h"
/*class Floor;*/

class Person{
public:
	Person(Floor &, Floor &);
	~Person();
	int getName();
	int getOrigin();
	int getDestination();
	
	void boardElevator();
	void exitElevator();
	
	
	// static memeber function
	static int getCount(); // return # of objects instantiated
	
private:
	int name;
	Floor &origin;	 
	Floor &destination;
	
	//static data member
	static int count;
};

#endif


// person.c++
// see person.h for class declaration

#include <iostream.h> 
#include "person.h"
#include "button.h"
#include "floor.h"
#include "elevator.h"

// intitalize static data member
int Person::count(0);

// define the static member function that returns the 
// person count
int Person::getCount() { return count;}

// Constructor receives parameters for origin and destination
// and pushes the button on the floor created with the value of
// the origin's floor number as the argument
Person::Person(Floor &orig, Floor &dest)
	: origin(orig), destination(dest)
{
	++count;
	name = getCount();
	origin.setPassenger(this);
	origin.button.pushButton();
}

Person::~Person() {  }

int Person::getName() { return name; }
int Person::getOrigin() { return origin.getFloorNum(); }
int Person::getDestination() { return destination.getFloorNum(); }

void Person::boardElevator(){
	cout << endl << "person number " << getName() << " boards the elevator";
	origin.removePassenger();
	origin.elevator.setPassenger(this);
	cout << endl << "person number " << getName() 
		<< " presses the botton for floor " << getDestination();
	cout << endl << "bug check b";
	(getDestination() == 1) ? origin.elevator.button1.pushButton() : 
		origin.elevator.button2.pushButton();  // *SITE OF CRASH
	cout << endl << "bug check a";
}

void Person::exitElevator(){ 
	cout << endl << "person number " << getName() << " exits the elevator";
	delete this;
}


// button.h

//Prevent multiple inclusions of the header file
#ifndef BUTTON_H
#define BUTTON_H

class Floor;
class Elevator;
// The #include that foolows was replaced with the line
// directly above.  I am not sure that this is kosher
//#include "elevator.h"  

class Button {
public:
	Button(Floor &, Elevator &);
	void pushButton();
	void reset();
	int getState();

private:
	int state;
	Floor &floor;
	Elevator &elevator;
};

#endif


// button.c++
#include <iostream.h>
#include "elevator.h"
#include "button.h"

Button::Button(Floor &fl, Elevator &el)
	: elevator(el), floor(fl)
{
	state = 0; 
}

void Button::pushButton(){
	cout << endl << "Button on";
	state = 1;
	elevator.goTo(&floor);
}

void Button::reset() {
	cout << endl << "Button off";
	state = 0;
}

int Button::getState() { return state; }


// elevator class

#ifndef ELEVATOR_H
#define ELEVATOR_H

#include "button.h"
#include "bell.h"
#include "light.h"
#include "door.h"
class Building;
class Floor;
class Person;

class Elevator{
public:
	Elevator(Building &);
	void goTo(Floor *);
	void move();
	void setPassenger(Person *);
	Button button1;  // I think that these buttons are not being
	Button button2;  // constructed properly.  But I don't know why
private:
	// private methods
	void arrive();
	
	// members
	int status;	// zero is standing still
	Light light;
	int balanceToArrival;
	const int travelTime;	// set in constructor
	
	Building &building;
	Bell bell;
	Door door;
	
	Floor *destination;
	Floor *location;
	
	Person *passenger;
};

#endif


// elevator.c++

#include <iostream.h>
#include "elevator.h"
#include "building.h"
#include "floor.h"
#include "person.h"

// Constructor
Elevator::Elevator(Building &bldng)
	:travelTime(5),
	building(bldng),
	button1(building.floor1, *this),
	button2(building.floor2, *this)
{
	status = 0;
	location = &building.floor1;
	destination = 0;
	passenger = 0;
}

  
void Elevator::goTo(Floor *call)
{
	if(call == location){
		destination=location;
		arrive();
	}
	else if(!status){
 		destination = call;
		status = 1;
		cout << endl << "The elevator lurches toward floor " 
			<< destination->getFloorNum();
		balanceToArrival = travelTime;
	}
}
void Elevator::move(){
	
	if(!(--balanceToArrival))
		arrive();
}

void Elevator::setPassenger(Person *psngr) {passenger = psngr;}


void Elevator::arrive(){
	// stop moving
	status = 0;	
	bell.ring();
	// destination->bell.ring();
	
	light.toggle();
	// destination->light.toggle();
	
	door.open();
	// destination->door.open();
	
	if(destination->getFloorNum() == 1)
		button1.reset();
	else if(destination->getFloorNum() == 2)
		button2.reset();
	destination->button.reset();
	
	if(passenger){
		passenger->exitElevator();
		passenger = 0;
	}
	if(destination->passenger){
   	    cout << endl <<"bug check 1";
		destination->passenger->boardElevator();
		cout << "bug check 2";
		destination->removePassenger();
	}
	
	light.toggle();
	// destination->light.toggle();
	
	door.close();
	// destination->door.close();
	
	cout << "bug check 3";
	if(!status){ // standing still?  Check for active call buttons.
		if(building.floor1.button.getState())
			goTo(&building.floor1);
		if(building.floor2.button.getState())
			goTo(&building.floor2);
	}
}

// The headers for the balance of the classes follow


// bell.h Abstact data type declaration
// see bell.c++ for method definitions

// prevent multiple inclusion of the header file

#ifndef BELL_H
#define BELL_H

class Bell {
public:
 Bell();
 void ring();
};

#endif


// door.h header file for adt
// see door.c++ for interface definitions

#ifndef DOOR_H
#define DOOR_H

class Door {
public:
	Door();
	void open();
	void close();
	int getState();
private:
	int state;	// open or closed
};

#endif


// light.h abstract data type declaration
// see light.c++ for member implementation

#ifndef LIGHT_H
#define LIGHT_H

class Light{
public:
	Light();
	void toggle();
private:
	int status;
	int getStatus();
};

#endif


// floor.h Class header for Floor Class of the elevator simulator problem
// Prevent multiple inclusions of the header file

#ifndef FLOOR_H
#define FLOOR_H

#include "button.h"
#include "door.h"
#include "light.h"
#include "bell.h"
#include "elevator.h"
#include "person.h"

class Floor{
public:
	Floor(int, Elevator &); // constructor
	
	// Check members
	int getFloorNum();
	Person *getPassenger();	// returns null on empty floor
		
	// Set functions
	void setPassenger(Person *);
	void removePassenger();
	Button button;
	Elevator &elevator;
	Person *passenger;

private:
	// Private member function
	void setFloorNum(int);
	
	//Memebers
	int floorNum;
	Door door;
	Light light;
	Bell bell;
};

#endif


// building.h

#ifndef BUILDING_H
#define BUILDING_H


#include "floor.h"
#include "elevator.h"

class Building {
public:
	Building();
	int getCount() const;
	Floor floor1;
	Floor floor2;
	Elevator elevator;
private:
	const int floorCount;
	
};

#endif


// scheduler.h

#ifndef SCHEDULER_H
#define SCHEDULER_H

#include<stdlib.h>
#include<iostream.h>
class Person;
class Floor;

class Scheduler {
public:	
	Scheduler(Floor &, Floor &);
	void getArrival(int);

private:
	int currentTime;
	int arrivalTime;
	
	Floor &origin;
	Floor &destination;
};

#endif
	

// Elevator
// Main

#include <iostream.h>
#include "building.h"
#include "elevator.h"
#include "person.h"
#include "scheduler.h"
main(){
	
	int time = 0;
	Building arcincorp;
	Scheduler scheduler1(arcincorp.floor1, arcincorp.floor2);
	Scheduler scheduler2(arcincorp.floor2, arcincorp.floor1);
	
	cout << "Good Morning and Welcome to arcin.net's Corporate office. \n" 		
		<< "The current time is: " << time;
	
	for( ; time < 100; time++){
		cout << endl << time;	
		scheduler1.getArrival(time);
		scheduler2.getArrival(time);
		arcincorp.elevator.move();
	}
	
	cout << endl << "Simulation is complete" << endl;

	return 0;
}
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads