How do I update a bool in a .h file?

0

I have the following code in the .cpp file (not the main):

    switch (opc) {
    case 1:

        while (jugando) {
            //limpiar pantalla
            h.limpiar();

            //elimina scroll bar
            h.nosc();

            //dibuja marco
            h.draw();

        } 

            break;
}

I only leave the first case since it is the only relevant one. Then, in the h.draw () function, the only thing to do is draw a frame with gotoxy, I have the so-called

this->jugando = false;

After that line, it leaves the function and returns to the while, after that it should close the program since playing = false, however it runs again and again.

The bool playing it I have declared it in the class of the .h file, I do not know if it should include the whole file, but the statement is simple:

bool jugando;

I would really appreciate the help.

    
asked by Hector 12.08.2018 в 06:26
source

1 answer

0
  

After that line, it leaves the function and returns to the while, after that it should close the program since playing = false, however it runs again and again.

Review the code carefully:

while (jugando) {
    //limpiar pantalla
    h.limpiar();

    //elimina scroll bar
    h.nosc();

    //dibuja marco
    h.draw();
} 

You say that h.draw() calls this->jugando=false , but in while you are not looking at the state of h.jugando , but%% of%, that is, a variable that is either declared at the level of the function that contains the jugando or is a global variable.

As you well know (or you will learn right now), in C ++ you can have several variables with the same name as long as their scope is not exactly the same:

int a;

namespace test
{
  int a;
}

struct POO
{
  int a;
};

int func()
{
  int a;
}

In your case the problem you have is that the variable you update is not the same whose value you are checking in the while . The solution is not clear to me because there is hardly any code and I do not know if it is possible to verify the status of while directly or if it has to be done using some getter . The code should look something like this:

while (h.get_jugando()) {

Where h.jugando is replaced by the mechanism provided to read the status of get_jugando() .

    
answered by 12.08.2018 в 12:54