# Slot Game

• 09-24-2004
pizzapie
Slot Game
I've developed a slot machine game. It is perfect now except for 1 thing, the random numbers don't change. Have you got any advice?
Code:

```#include <iostream> #include <ctime> #include <cstdlib> using namespace std; int main() {     srand(time(0));     int num1=rand()%10+1;         srand(time(0));     int num2=rand()%10+1;         srand(time(0));     int num3=rand()%10+1;     int coins=10;     cout<<"---------------------------------------------------------------------"<<endl;     cout<<"                Welcome to slot machine"<<endl;     cout<<"                    You have 10 coins"<<endl;     cout<<"      If all 3 numbers that appear are the same, you get 10 coins"<<endl;     cout<<"          If 2 numbers are the same, you get 3 coins"<<endl;     cout<<"          If all 3 numbers are different, you lose 1 coin"<<endl;     cout<<"                    Hit enter to play"<<endl;     cout<<"---------------------------------------------------------------------"<<endl;cin.get();     cout<<""<<endl;     while(coins<100)     {     cout<<num1<<"    "<<num2<<"    "<<num3<<"    "<<endl;     if(coins<1)     {         coins=coins+10;         cout<<"You are bankrupt, your money is reset. You now have "<<coins<<endl;         cout<<"coins"<<endl;     }        else if(num1==num2&&num1==num3)     {         coins=coins+10;         cout<<"You got the jackpot. You now have "<<coins<<" coins"<<endl;cin.get();     }        else if(num1==num3||num1==num2||num2==num3)     {         coins=coins+3;         cout<<"You won 3 coins. You now have "<<coins<<" coins"<<endl;cin.get();     }     else     {         coins=coins-1;         cout<<"You lose 1 coin. You now have "<<coins<<" coins"<<endl;     } } cout<<"You have won the game. Congratulations.";cin.get(); return 0; }```
• 09-24-2004
Shakti
call srand only once in the program. And in that whole whileloop you have there you never change the values of num1, num2 or num3.
• 09-24-2004
CornedBee
I've edited it so that srand is called only once and the rand calls are placed inside the loop.
Code:

```#include <iostream> #include <ctime> #include <cstdlib> using namespace std; int main() {     int num1;     int num2;     int num3;     int coins=10;     srand(time(0));     cout<<"---------------------------------------------------------------------"<<endl;     cout<<"                Welcome to slot machine"<<endl;     cout<<"                    You have 10 coins"<<endl;     cout<<"      If all 3 numbers that appear are the same, you get 10 coins"<<endl;     cout<<"          If 2 numbers are the same, you get 3 coins"<<endl;     cout<<"          If all 3 numbers are different, you lose 1 coin"<<endl;     cout<<"                    Hit enter to play"<<endl;     cout<<"---------------------------------------------------------------------"<<endl;cin.get();     cout<<""<<endl;     while(coins<100)     {     num1=rand()%10+1;     num2=rand()%10+1;     num3=rand()%10+1;     cout<<num1<<"    "<<num2<<"    "<<num3<<"    "<<endl;     if(coins<1)     {         coins=coins+10;         cout<<"You are bankrupt, your money is reset. You now have "<<coins<<endl;         cout<<"coins"<<endl;     }        else if(num1==num2&&num1==num3)     {         coins=coins+10;         cout<<"You got the jackpot. You now have "<<coins<<" coins"<<endl;cin.get();     }        else if(num1==num3||num1==num2||num2==num3)     {         coins=coins+3;         cout<<"You won 3 coins. You now have "<<coins<<" coins"<<endl;cin.get();     }     else     {         coins=coins-1;         cout<<"You lose 1 coin. You now have "<<coins<<" coins"<<endl;     } } cout<<"You have won the game. Congratulations.";cin.get(); return 0; }```
• 09-24-2004
pizzapie
you guys are brilliant
• 09-24-2004
CornedBee
You still have one problem: If I have one coin, I insert it, I get results, but they are never evaluated. It tells me that I'm bankrupt and resets my coins, then another round is started.
• 09-24-2004
pizzapie
good point, ill get on it right away
• 09-24-2004
pizzapie
fixed
Code:

```#include <iostream> #include <ctime> #include <cstdlib> using namespace std; int main() {     int num1;     int num2;     int num3;     int coins=10;     srand(time(0));     cout<<"|------------------------------------------------------------------------------|"<<endl;     cout<<"|                          Welcome to slot machine                            |"<<endl;                        cout<<"|                                By Paul Leone                                |"<<endl;     cout<<"|                              You have 10 coins                              |"<<endl;     cout<<"|          If all 3 numbers that appear are the same, you get 10 coins        |"<<endl;     cout<<"|                  If 2 numbers are the same, you get 3 coins                  |"<<endl;     cout<<"|              If all 3 numbers are different, you lose 1 coin                |"<<endl;     cout<<"|                              Hit enter to play                              |"<<endl;     cout<<"|------------------------------------------------------------------------------|"<<endl;cin.get();     cout<<""<<endl;     while(coins<100)     {     num1=rand()%10+1;     num2=rand()%10+1;     num3=rand()%10+1;     cout<<num1<<"    "<<num2<<"    "<<num3<<"    "<<endl;     if(coins<1)     {         if(num1!=num2&&num1!=num3&&num2!=num3)         {             coins=coins+10;         cout<<"You are bankrupt, your money is reset. You now have "<<coins<<endl;         cout<<"coins"<<endl;cin.get();cin.get();     }            else if(num1==num2&&num1==num3)         {                 coins=coins+10;         cout<<"You got the jackpot. You now have "<<coins<<" coins"<<endl;cin.get();cin.get();     }     else if(num1==num2||num1==num3||num2==num3)     {                  coins=coins+3;         cout<<"You won 3 coins. You now have "<<coins<<" coins"<<endl;cin.get();cin.get();     }     else     {         coins=coins-1;         cout<<"You lose 1 coin. You now have "<<coins<<" coins"<<endl;cin.get();cin.get();     }     }        else if(num1==num2&&num1==num3)     {         coins=coins+10;         cout<<"You got the jackpot. You now have "<<coins<<" coins"<<endl;cin.get();cin.get();     }        else if(num1==num3||num1==num2||num2==num3)     {         coins=coins+3;         cout<<"You won 3 coins. You now have "<<coins<<" coins"<<endl;cin.get();cin.get();     }     else     {         coins=coins-1;         cout<<"You lose 1 coin. You now have "<<coins<<" coins"<<endl;cin.get();cin.get();     } } cout<<"You have won the game. Congratulations.";cin.get(); return 0; }```
• 09-24-2004
CornedBee
Unnecessary code duplication. It should be more like (in pseudo-code):
Code:

```main() {   init and welcome;   do {     generate and display random numbers;     if(jackpot) {       jackpot message;       coins += 10;     } else if(minor win) {       minor win message;       coins += 3;     } else {       loss message;       coins -= 1;       if(coins <= 0) {         bankrupt message;         reset coins;       }     }   } while(coins < 100); }```
• 09-24-2004
pizzapie
fair enough but i can't be arsed changing it, besides it doesn't matter because i decided to make it to see if i really understood the stuff i was learning. this proves that i do because i wasn't even learning random numbers