slot

this is the code i have so far for a slot machine i am working on. i feel like my functions are ok so far but i am having trouble in main. does anybody see where i need to make corrections? i am thinking i need to change my function calls to value returning instead of void..not sure how


#include <iostream>
#include "graphics.h"
#include <cstdlib>
#include <string>
#include <ctime>

using namespace std;

string getname(); //prototype
string getname() //header
{
string name;
getline (cin, name);
return name;
}

char getyorn(); //prototype
char getyorn() //header
{
char response;
cin >> response;
while (response != 'y' && response != 'n' && response != 'Y' && response != 'N')
{
cout << "Please enter a Y or N or y or n ";
cin >> response;
}
return response;
}

int getbet (int bet); //prototype
int getbet (int bet) //header
{
int bet1;
int betmax;
betmax= 200;
cout << "What do you want to bet? (up to $200) " << endl;
cin >> bet1;
while (bet1 > betmax)
{
cout << "You can't bet more than you have. What is your bet? ";
cin >> bet1;
if (bet1 <= 0)
{
cout << "You can't bet negative or zero. What is your bet? ";
cin >> bet1;
}

}
return bet1;
}

int getspin();
int getspin()
{
return rand() % 9 + 1;
}

int pullhandle();
int pullhandle()
{
int ct;
ct= 0;
cout << "Pulling the handle... " << endl;
while (ct < 3)
{
getspin();
if (getspin() == 1)
cout << "Apple ";
else if (getspin() == 2)
cout << "Cherry ";
else if (getspin() == 3)
cout << "Melon ";
else if (getspin() == 4)
cout << "Bar ";
else if (getspin() == 5)
cout << "Grape ";
else if (getspin() == 6)
cout << "Plum ";
else if (getspin() == 7)
cout << "JACKPOT ";
else if (getspin() == 8)
cout << "Orange ";
else
cout << "Lemon ";
ct++;
}
return 0;
}

int main()
{
srand(time(NULL));
int bet;
char answer;

cout << "One Armed Bandit!! " << endl;
cout << "What's your name? ";
getname();

cout << "You will start with 200 dollars " << endl;
cout << "Do you want to play with the One Armed Bandit? ";
cin >> answer;

if (answer != 'n' || answer != 'y')
getyorn();
if (getyorn() == 'n')
{
cout << endl << "Thank you for playing. Good luck. ";
}
while (getyorn() == 'y')
{
getbet(bet);
pullhandle();
cout << endl << "Do you want to play again? ";
getyorn();
if (getyorn() == 'n')
cout << endl << "Thank you for playing. Good luck. ";
}
return 0;
}
What are the errors? Also, put the code tags like this:

[code]
your code here
[/code]
i dont get errors but i do get something odd happening in main when the user is asked if they want to play the game..if i enter y or n or anything else the code skips a line and i have to press y or n or whatever again...sometimes 3 or 4 times before the code resumes..i thought it might be because i am not calling my functions correctly but im not sure..any ideas?

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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <iostream>
#include "graphics.h"
#include <cstdlib>
#include <string>
#include <ctime>

using namespace std;

string getname(); //prototype
string getname() //header
{
	string name;
	getline (cin, name);
	return name;
}

char getyorn(); //prototype
char getyorn() //header
{	
	char response;	
	cin >> response;
	while (response != 'y' && response != 'n' && response != 'Y' && response != 'N')
		{
			cout << "Please enter a Y or N or y or n ";
			cin >> response;
		}
	return response;
}

int getbet (int bet); //prototype
int getbet (int bet)  //header
{
	int bet1;
	int betmax;
	betmax= 200;
	cout << "What do you want to bet? (up to $200) " << endl;
	cin >> bet1;
	while (bet1 > betmax)
		{
			cout << "You can't bet more than you have.  What is your bet? ";
			cin >> bet1;
			if (bet1 <= 0)
			{
				cout << "You can't bet negative or zero.  What is your bet? ";
				cin >> bet1;
			}

		}
	return bet1;
}

int getspin();
int getspin()
{
	return rand() % 9 + 1;
}

int pullhandle();
int pullhandle()
{
	int ct;
	ct= 0;
	cout << "Pulling the handle... " << endl;
	while (ct < 3)
	{
		getspin();
		if (getspin() == 1)
			cout << "Apple ";
		else if (getspin() == 2)
			cout << "Cherry ";
		else if (getspin() == 3)
			cout << "Melon ";
		else if (getspin() == 4)
			cout << "Bar ";
		else if (getspin() == 5)
			cout << "Grape ";
		else if (getspin() == 6)
			cout << "Plum ";
		else if (getspin() == 7)
			cout << "JACKPOT ";
		else if (getspin() == 8)
			cout << "Orange ";
		else
			cout << "Lemon ";
		ct++;
	}
	return 0;
}

int main()
{
	srand(time(NULL));
	int bet;
	char answer;

	cout << "One Armed Bandit!! " << endl;
	cout << "What's your name? ";
	getname();
	
	cout << "You will start with 200 dollars " << endl;	
	cout << "Do you want to play with the One Armed Bandit? ";
	cin >> answer;
	
	if (answer != 'n' || answer != 'y')
		getyorn();
	if (getyorn() == 'n')
	{
		cout << endl << "Thank you for playing. Good luck. ";
	}
	while (getyorn() == 'y')
	{
		getbet(bet);
		pullhandle();
		cout << endl << "Do you want to play again? ";
		getyorn();
		if (getyorn() == 'n')
			cout << endl << "Thank you for playing. Good luck. ";
	}
	return 0;
}
getspin() returns a random number every time it is called. You are calling it 9 times in pullhandle(). I think you meant to call it once, save off the value it returned, and then compare that value against 1-8.

thanks...not sure how to do that..i tried changing the code around but it messed it up. also im still having the trouble in main of having to type the y o n multiple times. i captured the output here to show what i mean

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
One Armed Bandit!!
What's your name? bella09
You will start with 200 dollars
Do you want to play with the One Armed Bandit? y
y
y
y
What do you want to bet? (up to $200)
100
Pulling the handle...
Melon Cherry Plum
Do you want to play again? n
n

Thank you for playing. Good luck. 



also, does anybody body have any ideas how to incorporate showing the user they won and calculating their winnings? appreciate the help.
It's the same problem.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
cout << "You will start with 200 dollars " << endl;	
	cout << "Do you want to play with the One Armed Bandit? ";
	cin >> answer;
	
	if (answer != 'n' || answer != 'y')
		getyorn();
	if (getyorn() == 'n')
	{
		cout << endl << "Thank you for playing. Good luck. ";
	}
	while (getyorn() == 'y')
	{
		getbet(bet);
		pullhandle();
		cout << endl << "Do you want to play again? ";
		getyorn();
		if (getyorn() == 'n')
			cout << endl << "Thank you for playing. Good luck. ";
	}


Every time you call getyorn(), it waits for the user to enter y or n.

You are calling it several times.


First, you cin >> answer. This is the first 'y'.

Then, you call it a second time here: if( answer != 'y' || answer != 'n' ) getyorn();
(Draw out the truth table to convince yourself that your if() clause is always true.

Then, you call it a third time here: if( getyorn() == 'n' ).

Then, you call it a fourth time here: while( getyorn() == 'y' ).

This makes up the four times you have to enter 'y' to get your program to continue.

To fix this issue, as with the other issue I mentioned, you only want to call the function
once and save off the returned value. For example:

1
2
3
4
char answer = getyorn();
if( answer == 'n' ) {
   // ...
}

awesome...that worked. thank you. now another issue. i need to get 3 separate results from this code and use them later in a graphics portion of the code. what i have now only outputs the words and i can't figure out how to get and store 3 separate results. any ideas?

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
int pullhandle();
int pullhandle()
{
int ct;
int display;
ct= 0;
cout << "Pulling the handle... " << endl;
while (ct < 3)
{
display = getspin();
if (display == 1)
cout << "Apple ";
else if (display == 2)
cout << "Cherry ";
else if (display == 3)
cout << "Melon ";
else if (display == 4)
cout << "Bar ";
else if (display == 5)
cout << "Grape ";
else if (display == 6)
cout << "Plum ";
else if (display == 7)
cout << "JACKPOT ";
else if (display == 8)
cout << "Orange ";
else
cout << "Lemon ";
ct++;
}
return 0;
}
You could call the function three times to get three different results.

1
2
3
int reel1 = getspin();
int reel2 = getspin();
int reel3 = getspin();


The easiest way to output the mnemonic at that point would be something like

1
2
3
static const char* names[] = { "Apple", ... };

cout << names[ reel1 ] << ' ' ...;

i can't use the static const char because we haven't learned that yet. calling getspin() 3 times makes perfect sense but where would i call it at? in the pullhandle() function or main?
You can omit the "static".

Where to call it is up to you.
Topic archived. No new replies allowed.