// ElevatorSimulator.cpp : Defines the entry point for the console application.
ID: 3619733 • Letter: #
Question
// ElevatorSimulator.cpp : Defines the entry point for the console application.//
#include "stdafx.h"
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
#define frz system("pause")
const int SIZE = 3;
struct Elevator
{
int floor;
int callButton[SIZE]; //1 ==> button has been pushed, 0 ==> clear
int sendButton[SIZE]; //Same as above
string direction; //"up" or "down"
string doorState; //"open" or "closed"
};
void initialize(Elevator &);
void callElevator(Elevator &);
//callElevator will randomly generate a 0 or 1 for each floor's call button.
void sendElevator(Elevator &);
//sendElevator will randomly generate a 0 or 1 for each floor's send button.
void moveElevator(Elevator *);
void doorOpen(Elevator &);
void doorClose(Elevator &);
void elevatorState(Elevator &);
int main()
{
Elevator elevator;
bool exitProgram = false;
char exit = 'y';
srand(time(0));
initialize(elevator);
while (elevator.callButton[1] == 0 && elevator.callButton[2] == 0)
callElevator(elevator);
do
{
moveElevator(&elevator);
cout << "Exit elevator simulator? (y or n)" << endl;
cin >> exit;
cout << endl;
if (exit == 'y')
exitProgram = true;
}while (exitProgram == false);
frz;
return 0;
}
void initialize(Elevator &elevator)
{
int i;
elevator.floor = 0;
for (i=0; i < SIZE; i++)
elevator.callButton[i] = elevator.sendButton[i] = 0;
elevator.direction = "up";
elevator.doorState = "closed";
}
void callElevator(Elevator &elevator)
{
int i;
for (i=0; i < SIZE; i++)
elevator.callButton[i] += rand()%2;
}
void sendElevator(Elevator &elevator)
{
int i;
for (i=0; i < SIZE; i++)
elevator.sendButton[i] += rand()%2;
}
/*****************************************************************************************
* Enter the code for moveElevator here. This function looks for call or send button *
* pushes in the direction it is headed and goes to that floor where it calls the *
* doorOpen function. Do not have it do anything else. *
*****************************************************************************************/
/*****************************************************************************************
* Enter the code for doorOpen here. This function clears all button pushes for the *
* current floor, calls elevatorState so you can see if the simulator is performing *
* "sensibly", and then calls doorClose. It also has an additional cout that displays that*
* the door has opened, people have entered and exited, and the door is closing. *
* Do not have it do anything else. *
*****************************************************************************************/
/*****************************************************************************************
* Enter the code for doorClose here. It calls the functions callElevator and *
* sendElevator. Then it checks for button activity in the direction it is heading and *
* changes direction if no activity. It couts that the door is closed, it may be changing *
* direction, and the elevator is about to move. Then is calls elevatorState so you can *
* see how things are going. Do not have it do anything else. *
*****************************************************************************************/
void elevatorState(Elevator &e)
{
int i;
cout << " State of elevator -------------------------" << endl;
cout << "Floor = " << e.floor + 1 << endl;
cout << "Direction = " << e.direction << endl;
cout << "Door is " << e.doorState << endl;
for (i=0; i < 3; i++)
{
cout << "Floor " << i + 1 << " call button = " << e.callButton[i] << " send button = " << e.sendButton[i] << endl;
}
cout << "--------------------------------------------------------" << endl << endl;
frz;
}
Explanation / Answer
please rate - thanks not knowing what could or couldn't be changed I changed the prototype I messaged you about, and made some changes in the main-when the program reruns it didn't set the buttons, and the send button was never set, and you're while made no sense// ElevatorSimulator.cpp : Defines the entry point for the console application.
//
//#include "stdafx.h"
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
#define frz system("pause")
const int SIZE = 3;
struct Elevator
{
int floor;
int callButton[SIZE]; //1 ==> button has been pushed, 0 ==> clear
int sendButton[SIZE]; //Same as above
string direction; //"up" or "down"
string doorState; //"open" or "closed"
};
void initialize(Elevator &);
void callElevator(Elevator &);
//callElevator will randomly generate a 0 or 1 for each floor's call button.
void sendElevator(Elevator &);
//sendElevator will randomly generate a 0 or 1 for each floor's send button.
void moveElevator(Elevator &);
void doorOpen(Elevator &);
void doorClose(Elevator &);
void elevatorState(Elevator &);
int main()
{
Elevator elevator;
bool exitProgram = false;
char exit = 'y';
srand(time(0));
initialize(elevator);
//while (elevator.callButton[1] == 0 && elevator.callButton[2] == 0)
do
{callElevator(elevator);
sendElevator(elevator);
elevatorState(elevator);
moveElevator(elevator);
cout << "Exit elevator simulator? (y or n)" << endl;
cin >> exit;
cout << endl;
if (exit == 'y')
exitProgram = true;
}while (exitProgram == false);
frz;
return 0;
}
void initialize(Elevator &elevator)
{
int i;
elevator.floor = 0;
for (i=0; i < SIZE; i++)
elevator.callButton[i] = elevator.sendButton[i] = 0;
elevator.direction = "up";
elevator.doorState = "closed";
}
void callElevator(Elevator &elevator)
{
int i;
for (i=0; i < SIZE; i++)
elevator.callButton[i] += rand()%2;
}
void sendElevator(Elevator &elevator)
{
int i;
for (i=0; i < SIZE; i++)
elevator.sendButton[i] += rand()%2;
}
/*****************************************************************************************
* Enter the code for moveElevator here. This function looks for call or send button *
* pushes in the direction it is headed and goes to that floor where it calls the *
* doorOpen function. Do not have it do anything else. *
*****************************************************************************************/
void moveElevator(Elevator &e)
{int i;
bool done=true;
do
{done=true;
if(e.direction=="up")
{for(i=e.floor+1;i<SIZE;i++)
if(e.callButton[i]==1||e.sendButton[i]==1)
{e.floor=i;
doorOpen(e);
done=false;
}
}
else
{if(e.direction=="down")
for(i=e.floor-1;i>=0;i--)
if(e.callButton[i]==1||e.sendButton[i]==1)
{e.floor=i;
doorOpen(e);
done=false;
}
}
}while(!done);
}
/*****************************************************************************************
* Enter the code for doorOpen here. This function clears all button pushes for the *
* current floor, calls elevatorState so you can see if the simulator is performing *
* "sensibly", and then calls doorClose. It also has an additional cout that displays that*
* the door has opened, people have entered and exited, and the door is closing. *
* Do not have it do anything else. *
*****************************************************************************************/
void doorOpen(Elevator & e)
{e.callButton[e.floor]=0;
e.sendButton[e.floor]=0;
e.doorState = "open";
elevatorState(e);
cout<<"at floor "<<e.floor+1<<" the door has opened, people have entered and exited, and the door is closing. ";
doorClose(e);
}
/*****************************************************************************************
* Enter the code for doorClose here. It calls the functions callElevator and *
* sendElevator. Then it checks for button activity in the direction it is heading and *
* changes direction if no activity. It couts that the door is closed, it may be changing *
* direction, and the elevator is about to move. Then is calls elevatorState so you can *
* see how things are going. Do not have it do anything else. *
*****************************************************************************************/
void doorClose(Elevator &e)
{int i;
bool upcalls=false,downcalls=false;
if(e.direction=="up")
for(i=e.floor+1;i<SIZE;i++)
if(e.callButton[i]==1||e.sendButton[i]==1)
{upcalls=true;
}
if(e.direction=="down")
for(i=e.floor-1;i>=0;i--)
if(e.callButton[i]==1||e.sendButton[i]==1)
{downcalls=true;
}
if(e.direction=="up"&&upcalls==false)
e.direction="down";
else if(e.direction=="down"&&downcalls==false)
e.direction="up";
e.doorState = "closed";
elevatorState(e);
cout<<"at floor "<<e.floor<<" the door has closed ";
}
void elevatorState(Elevator &e)
{
int i;
cout << " State of elevator -------------------------" << endl;
cout << "Floor = " << e.floor + 1 << endl;
cout << "Direction = " << e.direction << endl;
cout << "Door is " << e.doorState << endl;
for (i=0; i < 3; i++)
{
cout << "Floor " << i + 1 << " call button = " << e.callButton[i] << " send button = " << e.sendButton[i] << endl;
}
cout << "--------------------------------------------------------" << endl << endl;
frz;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.