Slot Machine - code review

Hello! I have recently started programming C++ for college classes ( im in data structures now.. its getting pretty difficult) So I am trying to make little projects in my spare time to try to improve on what I have done.

Here I have made a 'slot' machine program that first calculates how much money you have then how much you wanna bet. You can win partially (2 of the 3 horizontally) win (3 horitonally) and diagonally.

I am looking to improve this code or look for ideas to add to it. I thought about trying to visual perspective with QT but I don't think im ready for that. Any thoughts and suggestions would help for this new kid on the block :)

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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <iostream>
#include <string>

using namespace std;

int main(){
int num1,num2,num3,num4,num5,num6,num7,num8,num9;
float amount,bet;

cout<<"How much money do you have? "<<endl;
cin>>amount;
string exit;
cout<<"|-----------------------------------------------------------|"<<endl;
cout<<"| Welcome to Taku's Slots where everyone is a winner!      |"<<endl;
cout<<"| the amount you win and lose will be kept along as you bet!|"<<endl;
cout<<"|-----------------------------------------------------------|"<<endl;


// This keeps from forcing you to play one slot if you dont wanna play immediately
do{      
cout<<"($"<<amount<<") "<<"Do you wish to play Slots? (type exit to close)"<<endl;
cin >> exit;
if (exit == "exit" || exit == "no"){break;}


// Betting and valid betting     
while((cout<<"How much would you like to bet on this slot? ")
	&& !(cin>>bet))
{
cout<<"Please Enter a Valid Bet "<<endl;
cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
}

if(exit !="exit"){
srand ( time(NULL) );
num1 = rand() % 9 + 1;
num2 = rand() % 9 + 1;
num3 = rand() % 9 + 1;
num4 = rand() % 9 + 1;
num5 = rand() % 9 + 1;
num6 = rand() % 9 + 1;
num7 = rand() % 9 + 1;
num8 = rand() % 9 + 1;
num9 = rand() % 9 + 1;

/* the slot machine visual */

cout<<"     |**********|"<<endl;
cout<<"     |   "<<"slots  |"<<endl;
cout<<"     |**"<<"**"<<"**"<<"**"<<"**|"<<endl;
cout<<"     | "<<num1<<" | "<<num2<<" | "<<num3<<"|"<<endl;
cout<<"     |--"<<"--"<<"--"<<"--"<<"--"<<"|"<<endl;
cout<<"     | "<<num4<<" | "<<num5<<" | "<<num6<<"|"<<endl;
cout<<"     |--"<<"--"<<"--"<<"--"<<"--"<<"|"<<endl;
cout<<"     | "<<num7<<" | "<<num8<<" | "<<num9<<"|"<<endl;
cout<<"     |--"<<"--"<<"--"<<"--"<<"--"<<"|"<<endl;

/* Deciding IF you win */

if (num4 == num5 && num5 == num6 && num4 == num6){
	cout<<endl<<"     DING DING DING! JACKPOT!"<<endl;
	cout<<"   You win double! you win "<<bet*2<<endl;
	amount = amount + (bet *2);
	}
else if (num4 == num5 || num5 == num6){
     cout<<endl<<"     DING DING WINNER!"<<endl;
	 cout<<"   You partial win you win $"<<bet*1.25<<endl;
	 amount = amount + (bet * 1.25);
	 }
else if (num1 == num5 && num5 == num9 && num1 == num9){
	cout<<endl<<"     DING DING DIAGONAL WIN"<<endl;
	cout<<"   You win ! you win $"<<bet*1.25<<endl;
	amount = amount + (bet *1.5);
	}
else if (num3 == num5 && num5 == num7 && num3 == num7){
	cout<<endl<<"     DING DING DIAGONAL WIN"<<endl;
	cout<<"   You win! you win $"<<bet*1.25<<endl;
	}
else if (num2 == num5 && num5 == num8 && num2 == num8){
	cout<<endl<<"     DING DING VERTICAL WIN"<<endl;
	cout<<"   You win ! you win $"<<bet*1.5<<endl;
	amount = amount + (bet *1.5);
	}	
else{cout<<endl<<"     Sorry Try Again"<<endl;
cout<<" You lose your bet of $"<<bet<<endl;
amount = amount - bet;

}
}}while(amount!= 0);{cout<<"Come back Again!"<<endl;}
system("pause");
return 0;

}
  
some ideas:

- check out strstream class for better formatting when printing your machine slots (cout stuff..)
- check out fstream to open a .txt file and save your money-amount (and load it next time you start the game instead of typing in an amount)
- maybe you could gather all those "int num1 - num8" in one array and then make a loop to create those random numbers (rand() % 9 + 1 stuff)

maybe that's enough for now =)
Thanks for the reply :) I will definitely look into those. I really like the money amount save. I thought about separating out the game to its own class but im not sure if thats really needed
Topic archived. No new replies allowed.