Home > Blockchain >  How do I fix an infinite while loop? C [closed]
How do I fix an infinite while loop? C [closed]

Time:10-05

One of the loops in the function loop infinitely. I believe it's the first one but I'm not sure where to start to fix it.

Code:

void computer_play (int& pile, bool smart)
{
    unsigned seed = time(0);
    srand(seed);
    int take = 0;
    int n = 0;
    bool pow_of_2n = false;
    while (n < pile)
    {
        if (pow(2, n) - 1 == pile)
        {
            pow_of_2n = true;
        }
        n  ;
    }
    if (smart == false)
    {
        take = rand() % (pile / 2)   1;
    }
    if (smart == true)
    {
        if (pow_of_2n == false)
        {
            while (n < pile)
            {
                if (pow(2, n) - 1 < pile && pow(2, n) - 1 >= pile / 2)
                {
                    take = pile - (pow(2, n) - 1);
                    n  ;                  
                }
            }
        }
        else
        {
            take = rand() % (pile / 2)   1;
        }
    }
    pile = pile - take;
    cout << "Computer plays: " << endl;
    cout << "The computer takes " << take << " marbles. " << endl;
}

CodePudding user response:

Look at this loop:

        while (n < pile)
        {
            if (pow(2, n) - 1 < pile && pow(2, n) - 1 >= pile / 2)
            {
                take = pile - (pow(2, n) - 1);
                n  ;                  
            }
        }

When both conditions of the if are true you assign to take and increment n. When either of them is false the loop does nothing and if n < pile is true it will never turn false.

To find such bugs you need to learn how to use a debugger.

CodePudding user response:

if you are not sure which of your loops is executing infinitely, you can try to cout or print out some logs, then you'll have an idea which part is having problem.

code :

while (n < pile)
{
    if (pow(2, n) - 1 == pile)
    {
        pow_of_2n = true;
    }
    n  ;
    cout << "problem is within the first loop" << endl;
}

and

while (n < pile)
{
    if (pow(2, n) - 1 < pile && pow(2, n) - 1 >= pile / 2)
    {
        take = pile - (pow(2, n) - 1);
        n  ;                  
    }
    cout << "problem is within the second loop" << endl; 
}

CodePudding user response:

I would like to explain the reason.

I have written a small test programm and call the functions 1000 times each with either true or false.

There is no problem, except a possible division by 0 if parameter pile is less than 2.

The real reason is that you hand over the variable pile via reference to your function. And "pile" will be modified. Your outer loop, there were you call your function pile, will then have its loop variable modified and run forever. So, this code

int main() {
    for (int i = 2; i< 1000;   i)
    computer_play(i, false);
}

will run forever.

To solve this, please change the signature of your function to:

void computer_play(int pile, bool smart)

and it will work.

Forget all other answers and especially the smart comments.

  •  Tags:  
  • c
  • Related