multi comparison in one condition

Aug 12, 2013 at 3:30pm
Hi!
I was doing a simple exercise (slot machine with only 3 possible values ). It took me less then a minute to do it. Here's the code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int random();

int number1;
int number2;
int number3;
int main()
{
    srand(time(NULL));
    cout<<"Welcome to the slot machine game! \n";
    cout<<"List of combinations \n";
    cout<<"1 1 1=1$ \n";
    cout<<"2 2 2=2$ \n";
    cout<<"3 3 3=3$ \n";
    cout<<"let's begin \n";
    cout<<"\n";
    do
    {
        number1=random();
        number2=random();
        number3=random();
        cout<<number1<<" "<<number2<<" "<<number3<<"\n";
        if (number1==1&&number2==1&&number3==1)
        {
            cout<<"You won 1$ \n";
        }
        else if (number1==2&&number2==2&&number3==2)
        {
            cout<<"You won 2$ \n";
        }
        else if (number1==3&&number2==3&&number3==3)
        {
            cout<<"You won 3$ \n";
        }
        else
        {
            cout<<"Try again \n";
        }
        char retry='y';
        cout<<"retry? (y/n) \n";
        cin>>retry;
        if (retry=='n')
        {
            break;
        }
    } while (true);
    return 0;
}
int random()
{
    return rand()%3+1;
}

After testing this program and seing it worked, I tried to simplify the if condition:
1
2
3
4
  if (number1==number2==number3==1)
        {
            cout<<"You won 1$ \n";
        }


I tested it and saw that it's not precise, and even non winning combinations (like 2 2 1 or 3 3 1) would make me win 1$. Can someone explain me why it doesn't work?
Thanks!

PS: I know that my "try again" isn't perfect because if someone enters another letter than 'y' or 'n' it would still repeat. I just didn't want to lose my time on this.
Aug 12, 2013 at 3:38pm
closed account (28poGNh0)
Only works with
1
2
3
4
 if (number1==1&&number2==1&&number3==1)
        {
            cout<<"You won 1$ \n";
        }

becuase comparaison between two statments return a boolean value if if statments get a true value excutes it contents if not skeep to the next

you here want to compare 4 statements! at once wich I think it is invalid
Last edited on Aug 12, 2013 at 3:45pm
Aug 12, 2013 at 3:42pm
closed account (3qX21hU5)
A easy way to shorten it would be just to test the variables against eachother. If number1 is equal to number2 and number2 is equal to number3 that means there was a winner. Then you can just print whatever number is in them variables to tell the user how much they have won.

For example.

1
2
3
4
if (number1 == number2 && number2 == number3)
{
    cout << "You won $" << number1 << "!" << endl;
}
Last edited on Aug 12, 2013 at 3:43pm
Aug 12, 2013 at 3:51pm
ok! Thanks to you two for your fast and good replies!
Aug 12, 2013 at 3:57pm
Can someone explain me why it doesn't work?

It doesn't work because of the order of evaluations.

 
(number1==number2==number3==1)

gets evaluated in several steps:

First, number1==number2 are compared. This results in a true or false boolean result. This result is then compared to number3, again resulting in a bool result. This bool result is then compared to the number 1.

Equivalent code:
1
2
3
4
temp_bool_result1 = (number1==number2) 
temp_bool_result2 = (temp_bool_result1 == number2)
temp_bool_result3 = (temp_bool_result2 == number3)
bool_result = (temp_bool_result3 == 1)





Aug 12, 2013 at 11:58pm
ok now I understand perfectly! Thanks AbstractionAnon!
Topic archived. No new replies allowed.