Code doesn't work

So here is a piece of my code. The point of this part is to set value of p2u1, p2u2 or p2u3 to zero, but the value before doing that must not be 0 (so it changes something) and it has to change only one value. However, this code doesn't do anything. The values entering equals values exiting and the compiler does not detect any error... Any idea what might be messed up here?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
a1 = 0;
        while (a1 = 0)
        {
        x2 = rand() % 3 + 1;

        if (x2 == 1 && p2u1 != 0)
        {
            p2u1 = 0;
            a1++;
        }

        if (x2 == 2 && p2u2 != 0)
        {
            p2u2 = 0;
            a1++;
        }

        if (x2 == 3 && p2u3 != 0)
        {
            p2u3 = 0;
            a1++;
        }
        }
One obvious error is the following:
 
while (a1 = 0)

Your code will never execute.
Line 2 sets a1 to zero and tests if it is non-zero, which will always be false.

I think you meant:
 
while (a1 == 0)

Note the comparison operator as opposed to the assignment operator.
Topic archived. No new replies allowed.