2

It's my first time creating a header and cpp file for an existing main. I can get the program to run, but there's no output. If anyone can help me troubleshoot, i would much appreciate it. The program is supposed to simulate an elevator. thanks!

Here is what i was given:

int main()

{

  elevator aLift(1);
 aLift.select(5);
  aLift.select(3);


  system("pause");
  return 0;
}

Here is my header file.

#ifndef elevator_h   
#define elevator_h  
#include <string>    
using namespace std; 



class elevator {
public: //operations

elevator();
//coonstructors
elevator (int initFloor);

//modifiers
void select (int newFloor);
//my floor is increased/decreased by difference.

//accessors
int getFloor() const;
//gets current floor number



private: //state
    int my_floor;
    int selected_floor;



};
#endif   // ifndef elevator_h

Lastly, here's my cpp file

#include "elevator.h"
#include <string>
#include <iostream> 
using namespace std; 


int selected_floor;
elevator;
elevator::elevator (int initFloor)
//coonstructors
{
    my_floor=initFloor;


}

    //modifiers
    void elevator::select (int)
    {
        while(my_floor < selected_floor)
    cout << "Going up to " <<  ++my_floor << endl;
    }
    //my floor is increased/decreased by difference.

    //accessors
    int elevator::getFloor() const
    { 
        return selected_floor;
    }

3 Answers3

0

Global variables are initialized 0, see here.

So your selected_floor is 0 and smaller as my_floor.

Edit:

At first glance I missed you have a member selected_floor and a global one. I think you don't need the global one.

What you are missing is:

// don't forget the default constructor
elevator::elevator ()
: my_floor(0)
, selected_floor(0)
{}

elevator::elevator (int initFloor)
: my_floor(initFloor)
, selected_floor(0)
{}

//modifiers
void elevator::select (int newFloor)
                         //^^^^^^^^
{
    selected_floor = newFloor;  // set you selected_floor to the value you want to select
  //^^^^^^^^^^^^^^^^^^^^^^^^^^
    while(my_floor < selected_floor)
    cout << "Going up to " <<  ++my_floor << endl;
}
user1810087
  • 5,146
  • 1
  • 41
  • 76
0

It will not execute because you have not initiated selected_floor and you are using it , it has a value of 0 . So when you are running the loop the "my_floor" which has a initial value of 1 is being tested against zero which is already greater than zero , that is there is no output on the screen .

What you should do is , first assign a value to selected_floor and then call the select function . you can assign the value to selected_floor in the select function itself before executing the loop .

    void elevator::select (int newFloor)
    {
        selected_floor = newFloor ;
        while(my_floor < selected_floor)
        cout << "Going up to " <<  ++my_floor << endl;
    }
abkds
  • 1,764
  • 7
  • 27
  • 43
0

There are couple of things not clear. From the cpp.

int selected_floor; // You have a global variable with the same name as a private variable. Anything specific you intend to do?

elevator; //This should not even compile.

What is this ? //modifiers void elevator::select (int)

Maybe you wanted something like this. void elevator::select (int floor) { selected_floor = floor;

Get rid of the global variable and make the change to ::select, it should be ok.