Slot Machine code issue

Feb 25, 2010 at 4:29am
Hi to everyone =) I am pretty new to c++ programming and tried to start making a slot machine console program to check if I could solve it on my own.

Sadly, even after looking a bit around google and this forum to find the answer to my question, I couldn't solve something : whenever my loop starts over again, my "chips" var goes back to its initial value without saving the gain you get from playing.

It's obviously something dumb I'm missing but well =/ can't figure.

My code used to be cleaner but as I went crazy on this specific issues, I added several useless thingies I'm too lazy to clean before everything works ;)

Btw, I get a warning C4244 when compiling about my srand(time(0)) though it doesn't affect the random results...

Here is my 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
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
96
97
98
99
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <ctime>

using namespace std;
int Random(int low, int high);
int Result(int a, int b, int c, int chips, int bet);

int main()
{
	srand( time(0) );
	int low1(2), low2(2), low3(2);    
	int high1(7), high2(7), high3(7);
	int menu=0;
	int bet =0;
	bool quit=0;
	cout << "Player's chips: $1000" /*<< chips */<< endl;
	int chips=1000;
	while (!quit)
	{
		cout << "1) Play slot. 2) Exit.";
		cin >> menu;
		switch (menu)
		{
		case 1:
			{
				cout << "Enter your bet: ";
				cin >> bet;
				if (bet<=0 || bet>chips)
				{
					cout << "You did not enter a valid bet." << endl;
					menu=1;
				}
				else 
				{
					int a = Random(low1,high1);
					int b = Random(low2,high2);
					int c = Random(low3,high3);
					cout << a << " " << b << " " << c << endl;
					cout << "Player's chips: $" << Result(a,b,c,chips,bet) << endl;
				}
			break;
			}
		case 2:
			{
				cout << "Exiting..." << endl;
				quit=1;
				break;
			}
		case 3:
			{
				cout << chips << endl;
				break;
			}
		default:
			{
				cout << "Not a valid menu !"<<endl;
			}
		}
	}
	if (chips <=0)
	{
		cout << "You have $0 ! Game Over !" << endl;
		quit=1;
	}
}


int Random(int low, int high)
{
 int random;
 random = low + rand() % ((high+1)-low);
 return random;
}
int Result(int a, int b, int c, int chips, int bet)
{
	if ((a==7 && b==7 && c==7))
	{
		cout << "You won 10 times your bet !($" << (bet*10) << ")" << endl;
		chips=chips+(bet*10);
	}
	if ((a==b==c) && !(a==7 && b==7 && c==7))
	{
		cout << "You won 5 times your bet !($" << (bet*5) << ")" << endl;
		chips=chips+(bet*5);
	}
	if ((a==b || a==c || b==c) && !(a==b==c) && !(a==7 && b==7 && c==7))
	{
		chips=chips+(bet*3);
		cout << "You won 3 times your bet ! ($" << (bet*3) << ")" << endl;
	}
	else 
	{
		cout << "You lost your bet ! ($-" << bet << ")" << endl;
		chips=chips-bet;
	}
	return chips;
}



Thanks to anyone who will help me !
Feb 25, 2010 at 4:58am
Result() does not change the value of "chips" for the caller. You need to assign the Result() to chips, then print the value of chips.
Feb 25, 2010 at 5:51am
I admit that I understand only half of your answer. I see why Result() won't change "chips", though I can't find out how to assign "Result()" to chips at the start of the loop when there's no inputs ? I tried modifying my code a bit but couldn't compile it while using Result() to manage chips.
Feb 25, 2010 at 6:06am
Assign the result to chips?
chips = Result(a,b,c,chips,bet);
Simple enough.
Feb 25, 2010 at 2:43pm
Yes. At line 41, instead printing "Result(...)" and otherwise discarding the result, add a line that does "chips = Result(...)" then print the updated value of chips.
Feb 25, 2010 at 5:06pm
Thanks a lot to the both of you =)

I guess lack of sleep is not really suitable with trying to understand stuff.

It's working neatly now !
Topic archived. No new replies allowed.