Nested do-while loops in if-else if statements bugging out

I'm a very beginner programmer writing a program to simulate a modified craps game, and I can't find out what order (if the order is even the problem) to arrange my nested do-while loop and my if-else if statements. I'll get a bug occasionally wherein the user will not be prompted to enter a bet amount, and the game will run on it's own using the last bet amount.

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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
 #include "stdafx.h"
#include <time.h>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <iostream>

using namespace std;
int newtotwin(int, int);
int newtotlose(int, int);
int betcase(int, int);
void rules();

int main()
{
	srand(time(NULL));
	int die;
	int pot = 50, bet;
	char repeat;
	rules();
	do
	{
		die = rand() % 11 + 2;
		cout << " Place your bet and press enter to roll." << endl;
		cout << " You have $" << pot << " to bet: ";
		cin >> bet;
		betcase(bet, pot);
		cout << endl<< " Rolling the dice..." << endl;
		if (die == 7 ||die == 11)
		{
			cout << "\tYou rolled a " << die << endl;
			cout << "\tYou're and instant winner!" << endl;
			cout << " Your new total is: $" << newtotwin(pot, bet) << endl;
			pot = newtotwin(pot, bet);
		}
			else if (die == 2 || die == 3 || die == 12)
			{
				cout << "\tYou rolled a " << die << endl;
				cout << "\tYou're a loser!" << endl;
				cout << "\tYour new total is: $" << newtotlose(pot, bet) << endl;
				pot = newtotlose(pot, bet);
			}
			else if ((die == 4 || die == 5 || die == 6 || die == 8 || die == 9 || die == 10));
			{
				int point = die;
				cout << "\tYou rolled a " << point
					<< " and must roll again!" << endl;
				cout << "\tRolling..." << endl;
				die = rand() % 11 + 2;
				if (die == 7)
				{
					cout << "\tYou rolled a " << "7" << endl;
					cout << "\tYou're a loser!" << endl;
					cout << "\tYour new total is: $" << newtotlose(pot, bet) << endl;
					pot = newtotlose(pot, bet);
				}
				else if (die == point)
				{
					cout << "\tYou rolled another " << point << "!" << endl;
					cout << "\tYou win!" << endl;
					cout << "\tYour new total is: $" << newtotwin(pot, bet) << endl;
					pot = newtotwin(pot, bet);
				}
				else
				{
					do {
						die = rand() % 11 + 2;
						cout << "\tYou rolled a " << die
							<< " and must roll again!" << endl;
						cout << "\tRolling..." << endl;
						if (die == point)
						{
							cout << "\tYou rolled another " << point << "!" << endl;
							cout << "\tYou win!" << endl;
							cout << "\tYour new total is: $" << newtotwin(pot, bet) << endl;
							pot = newtotwin(pot, bet);
						}
						else if (die == 7)
						{
							cout << "\tYou rolled a " << "7" << endl;
							cout << "\tYou're a loser!" << endl;
							cout << "\tYour new total is: $" << newtotlose(pot, bet) << endl;
							pot = newtotlose(pot, bet);
						}
					} while (!(die == 7 || die == point));
				}
			}
			if (pot <= 0)
			{
				cout << "\tYour ran out of momey!" << endl;
				cout << "\tBecause of your irresponsible gambling habits, you can no longer play..." << endl;
				system("pause");
				return 0;
			}
		cout <<endl<< " Would you like to play again?" << endl;
		cout << " Enter 'y' for yes or 'n' for no: ";
		cin >> repeat;
		cout << endl << endl;
	} while (repeat == 'y' || repeat == 'Y');
	while (repeat == 'n' || repeat == 'N')
	{
		cout << "\tThanks for playing.";
		return 0;
	}
	system("pause");
	return 0;
}

int newtotwin(int x, int y)
{
	x += y;
	return x;
}

int newtotlose(int x, int y)
{
	x -= y;
	return x;
}

int betcase(int x, int y)
{
	while (x > y)
	{
		cout << "\tYou do not have that much money!" << endl;
		cout << "\tPlease enter a valid amount: ";
		cin >> x;
		cout << endl;
	}
	while (x < 1)
	{
		cout << "\tThis game isn't free!" << endl;
		cout << "\tPlace a bet greater than $0: ";
		cin >> x;
		cout << endl;
	}
	return x;
}

void rules()
{
	cout << " Welcome to a simulated craps game!" << endl;
	cout << " The rules are as follows:" << endl << endl;
	cout << "\t****************************************************************" << endl;
	cout << "\t* You will start out with $50 to play with. You will be asked  *" << endl;
	cout << "\t* to place a bet and when you press enter the dice will roll   *" << endl;
	cout << "\t* and the game will begin. If you roll a 7 or an 11, you are   *" << endl;
	cout << "\t* an instant winner and the your bet will be added to your     *" << endl;
	cout << "\t* total. If you roll a 2, 3, or 12 you lose and your bet will  *" << endl;
	cout << "\t* be deducted from your total. If you roll anything else, you  *" << endl;
	cout << "\t* will have to roll again and try to match that roll. If you   *" << endl;
	cout << "\t* roll a 7 while trying to match the roll, you lose and your   *" << endl;
	cout << "\t* bet will be deducted from your total. However if you do match*" << endl;
	cout << "\t* the roll, you win and your bet will be added to your total.  *" << endl;
	cout << "\t****************************************************************" << endl << endl;
}
Last edited on
Well, at first glance, i question the need for lines 100, 101, 103, and 104. If they didn't answer "y", then they drop out of the loop anyway. Why loop for "n"?
Last edited on
Topic archived. No new replies allowed.