Aug 13, 2013 at 9:10pm UTC
I'm writting a program that would simulate the logic of an elevator. I was doing well, but then when I made some while loops to simulate the delays of the elevator opening it's doors and moving floors I ran into a problem. Although the syntax is right, the program does not seem to be reading the loops and I am not getting the delays. Here is the part of the code that is doing this. If someone could show me how to fix this that would be great.
//Pick Up System
if(Up1 == pushed){
cout << "Elevator has arrived on Floor One! Door opening!" << endl;
while(x < 200000000) {
cout << "Test" << endl;
x++;
}
cout << "Door has opened." << endl;
CurrentFloor = 1;
}
//elevator rising
cout << "Elevator rising to next qued level." << endl;
while(x < 200000000){
cout << "Test" << endl;
x++;
}
if(Up2 == pushed){
cout << "Elevator has arrived on Floor Two! Door opening!" << endl;
while(x < 200000000){
cout << "Test" << endl;
x++;
}
cout << "Door has opened." << endl;
CurrentFloor = 2;
}
cout << "Elevator rising to next qued level." << endl;
while(x < 200000000){
cout << "TEST" << endl;
x++;
}
if(Up3 == pushed){
cout << "Elevator has arrived on Floor Three! Door opening!" << endl;
while(x < 200000000){
cout << "TEST" << endl;
x++;
}
cout << "Door has opened." << endl;
CurrentFloor = 3;
}
}
Aug 13, 2013 at 9:22pm UTC
It is no clear what is the initial value of x in each loop and what type x has.
Aug 13, 2013 at 10:14pm UTC
I could put in the full code, I just thought this would be easier, sorry. Here is the full code, hopefully you can see all the variables a bit better.
#include <iostream>
using namespace std;
int main()
{
//stores that button was pushed.
int pushed = 1;
//Up buttons outside the elevator
int Up1;
int Up2;
int Up3;
int Up4;
int Up5;
//Keep track of current floor
int CurrentFloor;
//to end all loops
int x;
//Simulate people calling elevator
//check floor 1
cout << "If floor one has someone on it, press one. If not, press 0." << endl;
cin >> Up1;
if(Up1 == pushed){
cout << "Floor one has been qued!" << endl;
}
//check floor 2.
cout << "If floor two has someone waiting, press one. If not, zero." << endl;
//did someone call on floor 2?
cin >> Up2;
// If so...
if(Up2 == pushed){
cout << "Floor two has been qued." << endl;
}
//check F3
cout << "If floor 3 has someone waiting press one. If not, press 0." << endl;
cin >> Up3;
if(Up3 == pushed){
cout << "Floor three has been qued" << endl;
}
//check F4
cout << "If someone is on Floor 4 press one. If not press 0" << endl;
cin >> Up4;
if(Up4 == pushed){
cout << "Floor 4 has been qued!" << endl;
}
//check F5
cout << "If someone is on Floor 5, press one. If not press 0." << endl;
cin >> Up5;
if(Up5 == pushed){
cout << "Floor 5 has been qued!" << endl;
}
//Pick Up System
if(Up1 == pushed){
cout << "Elevator has arrived on Floor One! Door opening!" << endl;
while(x < 200000000) {
cout << "Test" << endl;
x++;
}
cout << "Door has opened." << endl;
CurrentFloor = 1;
}
//elevator rising
cout << "Elevator rising to next qued level." << endl;
while(x < 200000000){
cout << "Test" << endl;
x++;
}
if(Up2 == pushed){
cout << "Elevator has arrived on Floor Two! Door opening!" << endl;
while(x < 200000000){
cout << "Test" << endl;
x++;
}
cout << "Door has opened." << endl;
CurrentFloor = 2;
}
cout << "Elevator rising to next qued level." << endl;
while(x < 200000000){
cout << "TEST" << endl;
x++;
}
if(Up3 == pushed){
cout << "Elevator has arrived on Floor Three! Door opening!" << endl;
while(x < 200000000){
cout << "TEST" << endl;
x++;
}
cout << "Door has opened." << endl;
CurrentFloor = 3;
}
}
Aug 13, 2013 at 10:17pm UTC
You are not assigning any initial value to variable x.
Aug 13, 2013 at 11:06pm UTC
Wow, that was really obvious. Thank you. I feel kind of stupid now. -_- Oh well, thank you Vlad!
Aug 14, 2013 at 2:24am UTC
Just as a reminder can you use code tags to make your code more readable? When you copy and paste your code into the post box, go under format on your right and click on the two brackets the first box.
Aug 14, 2013 at 4:49pm UTC
Ok, I will next time, thank you.