Completing function for dice game

Need help completing the methods for the functions of getBet and wonTheBet.

Please help asap!




#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;


int main ()
{
srand(time(NULL));
int money = 100, bet;

while (money > 0) {

cout << "You currently have " << money << " dollars. \n";
bet = getBet(money);

if (wonTheBet()) {
int amount = bet * 2;
money += amount;
cout << "Congratulations! You just won " << amount << " dollars!\n\n";
}

else {
money -= bet;
cout << "Ah, you lost! Try again! \n\n";
}

}

cout << "You are out of money!\n";
return 0;
}


THANK YOU!
I'd say that in the function void getBet() you have to let the user type in an amount they want to bet (you can do this using cin) and thus set the variable int bet . You also need to let the user choose which side of the dice they want to bet on, which would be another variable.
The if-statement you have there at the moment doesn't make any sense, as you can do all this in the function void wonTheBet(). At the moment you also need to actually "roll the dice" and compare the value of the randomly chosen side to the one the user has his bet on. If its the same, call the function wonTheBet, if not call a function lostTheBet, which has the logic of your current else-statement.

I hope this helped you, if there are any more questions, feel free to ask ;).
Can you show me how you would write this out?
I need to complete it for homework...
Thanks
Last edited on
Here you go, but it's really likely that not everyone will paste completed code for you, so consider yourself lucky ;)

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
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

bool won;
void getBet();
void throwingDice();
void result();
float bet;
int faceBet;
int faceThrown;
int money = 100;
int faces = 6;

int main()
{

	

	while (money > 0) {

		cout << "You currently have " << money << " dollars. \n";
		getBet();

			if (won == true) 
			{
			int amount = bet * 2;
			money += amount;
			cout << "Congratulations! You just won " << amount << " dollars!\n\n";
			}

			else 
			{
			money -= bet;
			cout << "Ah, you lost! Try again! \n\n";
			}

			}

		cout << "You are out of money!\n";
		
		return 0;
	
}

	void getBet()
	{
		cout << "Please input the face you want to place your bet on:\n";
		cin >> faceBet;
		while (faceBet > faces)		//Just there so that the user can't input 10 or sth. like that
		{
			cout << "A dice has six faces, so please choose a number from 1 to 6!\n";
			cin >> faceBet;
		}
		cout << "Please tell us how much you want to bet on that face (" << faceBet << "):\n";
		cin >> bet;
		while (bet > money)  // checking whether the user wants to bet more than he has
		{
			cout << "You only have " << money << "$. Please bet an amount less than or equal to the money you have left.\n";
			cin >> bet;
		}
		
			cout << "Your bet is " << bet << "$ on Nr. " << faceBet << ".\n";
		
		throwingDice();
	}


	void throwingDice()
	{
		srand(time(NULL));
		int randomInt = rand() % 70;
		if (randomInt < 10)
		{
			faceThrown = 1;

		}

		if (randomInt > 9 && randomInt < 20)
		{
			faceThrown = 2;

		}
		if (randomInt > 19 && randomInt < 30)
		{
			faceThrown = 3;

		}
		if (randomInt > 29 && randomInt < 40)
		{
			faceThrown = 4;

		}
		if (randomInt > 39 && randomInt < 50)
		{
			faceThrown = 5;

		}
		if (randomInt > 49 && randomInt < 60)
		{
			faceThrown = 6;

		}
		result();
	}

	void result()
	{
		if (faceBet == faceThrown)
		{
			won = true;
		}
		else
		{
			won = false;
		}
	
	}
Topic archived. No new replies allowed.