#include <iostream>
#include <cstdlib>
#include <ctime>
usingnamespace 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";
}
elseif (number1==2&&number2==2&&number3==2)
{
cout<<"You won 2$ \n";
}
elseif (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.
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.
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.