Elevator service using Classes and Boolean variables
Apr 11, 2019 at 4:58am UTC
My code is supposed to service 3 floors while attempting to service a 4th floor, before going back down to the first floor. I have it set up that user input of "y/n" prompts the floor to move, however I've been having an issue where it only continues to go on "n" when I've tried to set to only return the current floor.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
// Description: elevator that services three floors
//starts 1st floor
//goes to second floor
//goes to third floor
//tries to go to fourth floor
//back to second floor
//back to first floor
#include <iostream>
#include <iomanip>
#include "Elevator.h"
using namespace std;
int main()
{
Floor beginClass;
Floor endClass();
beginClass.goUp();
beginClass.goDown();
system("pause" );
}
Floor::Floor()
{
int CurrentFloor = 1;
bool GoingUp = true ;
bool GoingDown = false ;
}
void Floor::goUp()
{
char ans;
GoingUp = true ;
do
{
cout << "Current Floor: " << CurrentFloor << endl;
cout << "Going up? y/n" << endl;
cin >> ans;
if (ans == 'y' , 'Y' )
{
++CurrentFloor;
}
else if (ans == 'n' ,'N' )
{
GoingUp == false ;
break ;
}
} while ((GoingUp == true ) && (CurrentFloor <= 3));
if (CurrentFloor = 4)
{
cout << "ERROR: Floor does not exist" << endl;
}
}
void Floor::goDown()
{
char ans;
GoingDown = true ;
do
{
cout << "Current Floor: " << CurrentFloor << endl;
cout << "Going down? y/n" << endl;
cin >> ans;
if (ans == 'n' , 'N' )
{
GoingDown = false ;
}
CurrentFloor--;
} while (GoingDown = true && CurrentFloor >= 1);
}
#include <iostream>
#include <iomanip>
using namespace std;
#ifndef FLOOR_H
#define FLOOR_H
class Floor
{
private :
int CurrentFloor = 1;
bool GoingUp;
bool GoingDown;
public :
Floor();
void goUp();
void goDown();
};
#endif
Apr 11, 2019 at 5:11am UTC
> if (ans == 'y', 'Y')
This isn't how you compare against a possible list of values.
You have to say something like
if ( ans == 'y' || ans == 'Y' )
> if (CurrentFloor = 4)
Watch out for all those =, where you should be using ==
Apr 11, 2019 at 5:15am UTC
On lines 55, 60, 84 you need to OR two logical comparisons, not write a comma between them.
On lines 68 and 93 the '=' should be "=='
Topic archived. No new replies allowed.