I need help with with this program

Write a program that asks the user what floor they are on, and what floor they want to go to. It then prints the sequence of floors that the elevator visits in its journey.

So, e.g. if the user is on the 6th floor and they are going to the 2st floor then the program says:
Now on 6
Now on 5
Now on 4
Now on 3
Now on 2
We are here!

So, e.g. if the user is on the 3rd floor and they are going to the 5th floor then the program says:
Now on 3
Now on 4
Now on 5
We are here!
Last edited on
This is what i have so far and it wont compile.


#include <iostream>
using namespace std;
int main ()
{
int n=6;
int i=2;
cout<<"What floor are you on?";
cin>>n;
cout<<"What floor do you want to go to?";
cin>>i;
do {
if (n!=i){
n--;
cout<<"You are on floor"<<n<<endl;
}
while (n!=i);

cout<<"We are here!";
system("pause");
return 0;
}
@mrtyson86

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
#include <iostream>
using namespace std;
int main ()
{
int n=6;// Don't really need to initialize n and i
int i=2;
cout<<"What floor are you on?";
cin>>n;
cout<<"What floor do you want to go to?";
cin>>i;
// First do comparison. Check if you're on a LOWER floor going UP
//        or on a HIGHER floor, going DOWN
// if ( n>i)
// increase n until N equals I
//if(n<i)
// decrease n
// if(n==i)
// cout << "you're already on that floor"
// If you still have problems, submit problem program, and we can help further
// And please use code tags ( shown on right while typing in message, looks like <> symbol
do {
if (n!=i){
n--;
cout<<"You are on floor"<<n<<endl;
}
while (n!=i);

cout<<"We are here!";
system("pause");
return 0;
}
ico
Also, treat yourself to descriptive variable names (instead of something generic like i or n) and use some whitespace to organize your code. The compiler won't care, but you will if you have to come back and study this code for an exam or something down the road.

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
#include <iostream>
using namespace std;
int main()
{
    int currentFloor;
    int destinationFloor; 

    cout << "What floor are you on?";
    cin >> currentFloor;
    cout << "What floor do you want to go to?";
    cin >> destinationFloor;

    if(currentFloor < destinationFloor)
    {
        //Going up!
        //create a loop that prints each floor as you ascend
    }
    else if (currentFloor > destinationFloor)
    {
        //Going down!
        //create a loop that prints each floor as you descend
    }
    else
    {
        //do nothing, we're already where we need to be!
    }

    cout << "We are here!";
    return 0;
}
Topic archived. No new replies allowed.