1. ## Slot Machine Problem

Slot Machine Problem

Hey there! I have begun to write a slot machine program/game. I have got it to work and the code looks like this:

Code:
```#include <iostream>
#include <ctime>
#include <cstdlib>
#include <string>
using namespace std;
int main()
{
srand(time(0));
int again;
int number1 = rand() % 3 + 1;
int number2 = rand() % 3 + 1;
int number3 = rand() % 3 + 1;
cout << number1 << number2 << number3 << '\n' << endl;
if (number1 != 3 || number2 != 3 || number3 != 2)
{
cout << "You lose!\n";
cout << "Try Again?\n";
cin >> again;
}
else
{
cout << "You won!\n";
}
if (again == 1)
{
cout << main();
}
if (again == 2)
{
EXIT_SUCCESS;
}
}```
But the problem is that I don't know how to have more than 1 number that's correct. Right now have I the number 332 but I want more numbers that's correct. At least 3 numbers. I tried to code the same lines but with another numbers in it but that didn't worked. Also coded I this line once more with another number:
Code:
```    if (number1 != 2 || number2 != 3 || number3 != 3)
{
cout << "You lose!\n";
cout << "Try Again?\n";
cin >> again;
}
else
{
cout << "You won!\n";
}```
Please can someone tell me how to do that?.

2. It took me a while to figure out your definition of 'slot machine' and therefore understand the problem. I'm guessing there's only 1 number that's correct (332) and you are supposed to (randomly) generate those exacly numbers in order to win, am I right?

3. I think you need to recheck your logic.
Code:
`if (number1 != 2 || number2 != 3 || number3 != 3)`
This statement will evaluate to true if any one of the checks evaluate to true. You only want it to execute if all of the checks evaluate to true.

Jim

4. I made one myself and it worked out for 2 numbers like this.

Code:
```#include <iostream>#include <ctime>
#include <cstdlib>
#include <string>
using namespace std;
int main()
{
srand(time(0));
int again;
int number1 = rand() % 3 + 1;
int number2 = rand() % 3 + 1;
int number3 = rand() % 3 + 1;
cout << number1 << number2 << number3 << '\n' << endl;
if ((number1 == 3 && number2 == 3 && number3 == 2) || (number1 == 3 && number2 == 3 && number3 == 3))
{
cout << "You won!\n";
}
else
{
cout << "You lose\n";
}
}```
This works well for 2 different numbers, I guess you just have to add a 3rd one to the code and voila.

5. Never call the main function!
The main function is NOT supposed to be recursive!

Code:
`cout << main();`
Tim S.

6. Just store your numbers in a list somewhere, then you can have as many as you want:
Code:
```#include <cstdlib>
#include <iostream>
#include <string>

namespace {
int hit[][3] = {
{1, 2, 3},
{2, 2, 3},
{3, 2, 3},
};
}

int main()
{
std::string buf;

do {
int a = 1 + rand() % 3;
int b = 1 + rand() % 3;
int c = 1 + rand() % 3;

std::cout << a << ' ' << b << ' ' << c << std::endl;

// Check for a hit
for (std::size_t i = 0; i < sizeof hit / sizeof *hit; i++) {
if (a == hit[i][0] && b == hit[i][1] && c == hit[i][2]) {
std::cout << "Winner!" << std::endl;
}
}

std::cout << "Again? (EOF to quit): ";
} while (getline(std::cin, buf));
}```

7. Khabz thanks! Your code where very clear to read for me for I am a beginner with C++ so that helped me very much thank you!.
Prelude I haven't got so far with C++ yet so I didn't understand all of your code. But I tested your code and it worked well!.
stahta01 if it is a dumb idea to call the main() function what can I call instead of main()?

8. Originally Posted by DecoratorFawn82
stahta01 if it is a dumb idea to call the main() function what can I call instead of main()?
Nothing. Calling main() is the worst way to restart the program. Instead you should use a loop like Prelude did to restart the slot machine. It would be even better if playing the slot machine was its own function, then you can just call that in a loop.

9. Calling main() is the worst way to restart the program.
Mostly because it's technically illegal in C++. Some compilers support it as a historical artifact, but even in C (the language that C++ inherited that feature from) it's exceedingly poor style.