0

could some one help. getting error with these 2 lines of code. num_red - count_red = red_pot;// all defined as 0 and

while (count_red = 0 && count_yellow = 0 && count_green = 0 && count_brown = 0 && count_blue = 0 && count_pink = 0)
        {
            if (count_black = 0)
            {
                score = score + 7;
                printf("Score: %d\n", score);
                num_balls = num_balls - 1;
            }

        }
Joel Spolsky
  • 33,372
  • 17
  • 89
  • 105
  • 5
    Almost everywhere you have `=`, you want `==`. – Anthony Pegram Nov 02 '11 at 03:05
  • 2
    You cannot assign a value to the expression `num_red - count_red`. The expression's value is dictated by arithmetic rules, not your code. – Matt Ball Nov 02 '11 at 03:06
  • Anthony- that was a silly mistake on my part. thank you, –  Nov 02 '11 at 03:11
  • Matt- any ideas on how i could work around num_red - count_red = red_pot –  Nov 02 '11 at 03:12
  • 1
    @user, how about write in plain language what you want that expression to be. Then we might be able to tell you how to write it. – Anthony Pegram Nov 02 '11 at 03:14
  • im detecting snooker balls. when im detecting the colors i have num_red incrementing by 1 in a loop every time a red in detected. when a ball is potted, programme counts the reds.the reds that were on the table(red_num) - the reds on the table now(count) = the number of red balls potted if any(red_pot) –  Nov 02 '11 at 03:19
  • @user170705: I've added to my answer with the way to do this setting. You basically just have to set the unknown value based on the two known ones. – paxdiablo Nov 02 '11 at 03:21
  • So you are saying red balls potted equals all red balls minus the red balls now on the table? That is `red_balls_potted = all_red_balls - red_balls_on_table;` In code, it is not `1 + 1 = 2`, it is `2 = 1 + 1;`, or rather `z = x + y;`. The result from the right is assigned to the variable on the left. – Anthony Pegram Nov 02 '11 at 03:21

1 Answers1

5

If that's a C-like language, you need to use == for equality checks, not =. The single = is for assignment so that:

int seven = 7;
int five = 5;
if (seven - five == 2) ...

is okay, but:

int seven = 7;
int five = 5;
if (seven - five = 2) ...

will, even if it compiles, not do what you expect.

You have a classic example in your code. The segment:

if (count_black = 0) blah;

will not execute blah when count_black is zero. It will set count_black to zero and steadfastly refuse to ever execute blah, since the result of count_blah = 0 is 0 (false).


If you want the equality:

num_red - count_red == red_pot

to be true, you need to assign one of those variables (the "unknown" one) based on the other two "known" ones. For example, if num_red and count_red are known, set red_pot with:

red_pot = num_red - count_red;

Alternatively, if red_pot and count_red are known, set num_red with:

num_red = count_red + red_pot;
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • thank you all very much. the errors are now gone. much appreciated. cheers guys –  Nov 02 '11 at 03:22