I am trying to simulate a elevator program and i have 2 classes, 1 elevator class and 1 scheduler class. Strange thing is when i run my prog, the variable for "current floor" which i stored in elevator class is always reset to -858993460 even though i reset it using a function to 0. Declaring it as static either doesn't seem to work or i do not really know how to declare it. Can someone pls help me?


#include <iostream.h>
#include <stdlib.h> //for the rand and srand function
#include <time.h> //for the time function
#include <conio.h> //for the getche in the pause

class Elevator{
public:
void up(); //move up function
void down(); //move down function
int getfloor(); //get current floor status
void setfloor(int floor); //sets the floor
void open(); //checks and opens the door function
void close(); //checks and closes the door function
int getdoor(); //get current door status
void setdoor(int door); //sets the foor status
void bell(); //mux i say more?
void reset(); //reset button to reset elevator
int door;
int floor;
int pause(void); //pause function
int cfloor; //current floor
int cdoor; //current door status

private:

};


#include "elevator.h"

void Elevator::reset(){ //resets it to 1st floor and closes the door
setdoor(0); //sets to close
setfloor(0); //sets to 1st floor
cout<<"Elevator::reset->Resetting elevator\n";
cout<<"Elevator::reset->Current floor of elevator is "<<getfloor()<<endl;
cout<<"Elevator::reset->Current door of elevator is "<<getdoor()<<endl;
pause();

}


void Elevator::up(){ //going up
cout<<"Elevator::up->cfloor value is "<<cfloor<<endl;
if(cfloor==0){
cout<<"Elevator::up->This msg only happens when cfloor is 0 or elevator is on 1st floor"<<endl;
cout<<"Elevator::up->Current floor is 1st floor\n";
if(cdoor==0){ //sets this condition becoz of the reset
cout<<"Elevator::up->This msg shows up when cdoor is 0 and its gonna invoke the open()"<<endl;
open(); //door may be close on 1st floor due to reset
}
close(); //closes the door
setfloor(1); //sets floor to 2nd floor
cout<<"Elevator::up->This shows when setfloor(1) is used"<<endl;
cout<<"Elevator::up->Current floor is "<<getfloor()<<endl;
cout<<"Elevator::up->Second floor reached\n";
open(); //opens the door
bell(); //sounds the bell and light

}
else {
cout<<"Elevator::up->This msg shows up when cfloor is 1 which means elevator is on 2d floor"<<endl;
cout<<"Elevator::up->Current floor is 2nd floor, we are at the top!\n";
setfloor(1);
}
}

void Elevator::down(){ //going down
cout<<"Elevator::down->cfloor value is "<<cfloor<<endl;
if(cfloor==1){
cout<<"Elevator::down->This msg only happens when cfloor is 1 or elevator is on 2nd floor"<<endl;
cout<<"Elevator::down->Current floor is 2nd floor\n";
close();
setfloor(0);
getfloor();
cout<<&quot;Elevator::down->First floor reached\n";
open();
bell();

}
else {
cout<<"Elevator::down->This msg only happens when cfloor is 0 or elevator is on 1st floor"<<endl;
cout<<"Elevator::down->Current floor is 1st floor, we are at the bottom!!!!\n";
//setfloor(0);
}
}

int Elevator::getfloor(){ //get current floor
cout<<"Elevator::getfloor->Currentfloor is "<<cfloor<<endl;
return cfloor;
}

void Elevator::setfloor(int floor){ //sets the floor
cfloor = floor;
cout<<"Elevator::setfloor->cfloor is "<<cfloor<<" floor is "<<floor<<endl;

}

void Elevator::open(){ //opens door

cout<<"Elevator::open->Open invoked"<<endl;
cout<<"Elevator::open->cdoor value is "<<cdoor<<endl;
if(cdoor==0){ //checks condition
cout<<"Elevator::open->If this msg shows, means its running under cdoor is 0 condition"<<endl;
cout<<"Elevator::open->Currently door is closed so we open it\n";
setdoor(1);
cout<<"Door opened\n";
}
else {
cout<<"Elevator::open->If this msg shows, means its running under cdoor is 1 condition"<<endl;
cout<<"Elevator::open->Currently door is opened so we can't open it again!\n";
}
}

void Elevator::close(){ //closes door
cout<<"Elevator::close->Closing invoked"<<endl;
cout<<"Elevator::close->cdoor value is "<<getdoor()<<endl;
if(cdoor==1){
cout<<"Elevator::close->If this msg shows, means its running under cdoor is 1 condition"<<endl;
cout<<"Elevator::close->Currently door is opened so we close it\n";
setdoor(0);
cout<<"Elevator::close->Door closed\n";
}
else {
cout<<"Elevator::close->This msg shows that cfloor is 0 and door is closed."<<endl;
cout<<"Elevator::close->Currently door is closed so we can't close it again!\n";
}
}

int Elevator::getdoor(){ //gets door status
return cdoor;
}

void Elevator::setdoor(int door){ //sets door status
cdoor = door;
cout<<"Elevator::setdoor->cdoor is "<<cdoor<<" door is "<<door<<endl;

}

void Elevator::bell(){ //bell and light
cout<<"Elevator::bell->Ding\n";
pause();
}

int Elevator::pause(void){

getche(); //this is to wait for a key to be pressed
return 0;
}




#include <iostream.h>
#include <stdlib.h>
#include "elevator.h"
#include <conio.h>
#include <time.h>

class random1 : public Elevator{ //inherit elevator class to use its functions like elevator::up()...
public:
int showtme();
int ctme();
int cact();
void reset();
void rnd(int a);
void tme(int rnum);
void checker(int perlvl, int elelvl);
void act(int state);


private:
int curtime;
int rnum;
int state;
int perlvl;
int elelvl;

};



#include "random1.h"

int random1::showtme(){
return rnum;}

void random1::rnd(int a){
rnum = rand()%a;
cout<<"Random::rnd->rnum is "<<rnum<<endl;

}

void random1::reset(){
curtime=0;
}

void random1::tme(int rnum){
curtime=curtime+rnum;
cout<<"Random::tme->Current Time is"<<curtime<<endl;
//return 0;
}

int random1::ctme(){
return curtime;}

void random1::checker(int perlvl, int elelvl){
if(perlvl==0 && elelvl==1){
cout<<"Random::checker->state 1->person is on first floor\n";
cout<<"Random::checker->state 1->elevator is on second floor so we are calling it down\n";
state = 1;}

else if (perlvl ==0 && elelvl==0){
cout<<"Random::checker->state 2->elevator is on first floor\n";
state = 2;}

else if (perlvl==1 && elelvl==0){
cout<<"Random::checker->state 3->person is on second floor\n";
cout<<"Random::checker->state 3->elevator is on first floor so we are calling it up\n";
state = 3;}

else if (perlvl==1 && elelvl==1){
cout<<"Random::checker->state 4->elevator is on second floor\n";
state = 4;}

}

int random1::cact(){
cout<<"Random::cact->State is "<<state<<endl;
return state;
}

void random1::act(int state){
cout<<"Random::act->state is "<<state<<endl;
switch(state){
case 1: Elevator::down();
Elevator::up();
break;
case 2: Elevator::up();
break;
case 3: Elevator::up();
Elevator::down();
break;
case 4: Elevator::down();
break;
}
}



int main(){
Elevator e; //creates elevator object
random1 o;
srand(time(NULL)); // seeds the random number generator with time


int r;
int i;
int l;
int perlvl, elelvl;

cout<<"Main->Initiating Reset\n";
e.reset(); //resets the elevator
o.reset();

for(i=0;i<60;){
o.rnd(15);
r = o.showtme(); //this gets the value of rnum from random1 class
o.tme(r);
i = o.ctme();
cout<<"Main->i value is "<<i<<endl;
o.rnd(2); // used to generate a number of 0 or 1 which is to determine the lvl person is on
perlvl = o.showtme();
cout<<"Main->Person lvl is "<<perlvl<<endl;

elelvl = e.getfloor();
cout<<"Main->Elelvl is "<<elelvl<<endl;
o.checker(perlvl, elelvl); //checks condition
l=o.cact();
o.act(l); //action to perform on each condition
cout<<"Main->value of rnum is"<<r<<endl;



}
return 0;
}