1

I'm struggling with my "IF" comparison between 3 variables. The program is supposed to comapare wheels 1, 2 and 3, and reward the player with £5 if all 3 are the same, and £2 if 2 of the 3 are the same.

However sometimes it doesn't work ( gives me £5 even though none are the same)

Please could someone take a look and tell me how to fix it?

#include <stdio.h>
#include <time.h>
#include <stdlib.h>

const char *symbol[4] = {"  bell   ", " orange  ", " cherry  ", "horseshoe"}; 

int main ()
{
  srand(time(NULL));
  int playagain =1;
  int wheel_1,wheel_2,wheel_3;
  int i;
  int money=10;
  int j = (rand()%30)+10;
  int start=1;
  int k;
  printf("you have £%d. Press any key to gamble £1\n", money);
  getchar();

  while(playagain == 1) 
  {

    for (i=0; i<j; i++)
    {
      printf("\033[2J\033[0;0f");
      printf("\033[%d;%df", 0, 0);
      wheel_1 = (rand()%4);
      wheel_2 = (rand()%4);
      wheel_3 = (rand()%4);
      printf("%s     ", symbol[wheel_1]);
      printf("%s     ", symbol[wheel_2]);
      printf("%s     \n", symbol[wheel_3]);
      for (k=0; k<10000000; k++)
      {
      }
    }
    printf("%d, %d, %d\n", wheel_1, wheel_2, wheel_3);
    if (wheel_1 == wheel_2 == wheel_3)
    { 
      printf("Congrats! You win £5\n");
      money = (money+5)-1;
    }
    else if (wheel_1 == wheel_2 ||
      wheel_1 == wheel_3 ||
      wheel_2 == wheel_3 )
    {  
      printf("Congrats! You win £2\n");
      money = (money+2)-1;
    }
    else
    {
      printf("Unlucky, you have lost your £1\n");
      money--;
    }

    printf ("You now have £%d\n", money);

    char *replay;
    printf ("would you like to play again?");
    scanf ("%s", &replay);

    if (strcmp(&replay, "yes") == 0) 
    {
      playagain = 1;
    } 
    else
    {
      playagain = 0;
    }
  }

}
Uyghur Lives Matter
  • 18,820
  • 42
  • 108
  • 144
iHazzam
  • 11
  • 1
  • 1
    Very similar to [this question](http://stackoverflow.com/q/6961643/827263). `x == y == z` is equivalent to `(x == y) == z`; `x == y` yield `0` or `1`, which is then compared for equality to `z`. – Keith Thompson Nov 26 '13 at 17:40
  • Use `(x & y & z) == (x | y | z)`. ;) – Hot Licks Nov 26 '13 at 18:52

3 Answers3

2

The way you are using if to compare wheels is wrong

if (wheel_1 == wheel_2 == wheel_3)// Wrong

You should be using

if ((wheel_1 == wheel_2 ) && (wheel_2 == wheel_3))

Almost everything in C returns a value. The expression wheel_2 == wheel_3 also returns a value. The value returned by this expression compares equal to zero if the condition is false else it is non zero(will be 1). When you do wheel_2 == wheel_3 and suppose the expression is true then it will return 1. This is again compared with wheel_1 like wheel_1 == 1.

1

As above, it looks like you need:

if((wheel_1 == wheel_2) && (wheel_2 == wheel_3)) 

Let us know if it works like this :-).

Jonathan
  • 147
  • 1
  • 2
  • 10
0

Yes, in here you would have to keep the comparison values differently, beacuse that x=y=z would return a value and that would be true even though they are not.

Hence you keep them as in associate property if(x==y) && if(y==z) so you naturally get x=z.

Ram Mehta
  • 449
  • 1
  • 6
  • 20