Weird glitch/ error in my console app

I recently wrote a Blackjack card game console application in Dev C++, for the most part it works fine, however sometimes i get this glitch where it says i lost even though my number was higher than the cpu's number, i believe that the code i have written is correct and this should not happen and this also doesnt happen every time , Please note that I'm very new to programming, as I just started almost a week ago, and I'm still in the process of watching tutorials online, so I know that I have a long way to go. Also, I'm not very experienced with the while loop. Another glitch ive seen is where it will tell me that i went over 21 so i lost, but I wasn't over 21, Ill hit and my total will be like 20 and it will tell me that I went over.
Heres the code for my game.

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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#include <iostream>
#include <cstdlib>
#include <time.h>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */\
int value2 = 0;
int goal;
int total;
int value;
int choice2;
int choice3;
int bet1;
int iscore = 500;
int number;
int number2;
int cpu;
int main(void) {
	srand(time(NULL));
	number=(rand()%21+2);
	number2=(rand()%10+2);
	cpu=(rand()%21+2);
	cout << "Welcome to Blackjack!" << endl;
		cout << "Enter your goal (higher than 500): " << endl;
		cin >> goal;
		cout << "Enter your bet:" << endl;
		cin >> bet1;
		while (bet1 >= goal)
		{
				cout << "It's not wise to bet your goal or to bet over it!\nEnter your bet:" << endl;
		cin >> bet1;
		}
		cout << "Your total is: " << number  << endl;
		cout << "Earnings: " << iscore << " Goal: " << goal << "\n(1)Hit\n(2)Stay" << endl;
		cin >> choice2;
		if (choice2 == 1)
		{
			(total = number + number2);
			cout << "Your new total is: " << total << endl;
			cout << "Your opponent's total is: " << cpu << endl;
			if (total > 21)
			{		
		                 value2 = 1;
			}
				if (total == cpu)
			{
			cout << "It's a tie!" << endl;
			cout << "Your earnings are still: " << iscore << " Your goal is: " << goal << endl;
			}
			if (number > cpu)
			{
				cout << "You win!" << endl;
				cout << "Your earnings are now: " << (iscore =bet1 + iscore) << " Your goal is: " << goal << endl;
						if (iscore >= goal)
			{
				cout << "Congratulations! You have hit it big in Vegas and have reached your goal!" << endl;
				return 0;
			}
			
			}
			if (value2=1 and number > cpu)
			{
				cout << "JUST KIDDING! You went over 21. You lose." << endl;
						cout << "Your earnings are now: " << (iscore=iscore - bet1) << " Your goal is: " << goal << endl;
			}
			if (number < cpu)
			{
				cout << "You lose." << endl;
				cout << "Your earnings are now: " << (iscore=iscore - bet1) << " Your goal is: " << goal << endl;
			
			if (iscore < 0)
			{
				cout << "Your dreams of hitting it big in Vegas are shattered. Game Over." << endl;
				return 0;
			}
			}
				cout  << "Would you like to play again?\n(1)Yes\n(2)No" << endl;
			cin >> choice3;
			if (choice3 == 2)
			{
				cout << "Thanks for playing!" << endl;
				return 0;
			}
			if (choice3 == 1)
			{
				int value=1;
			}
		}
		if (choice2 == 2)
		{
			cout << "Your total is still: " << number << endl;
			cout << "Your opponent's total is: " << cpu << endl;
			if (number == cpu)
			{
				cout << "Its a tie!" << endl;
					cout << "Your earnings are still: " << iscore << " Your goal is: " << goal << endl;
			if (number > cpu)
			{
				cout << "You win!" << endl;
				cout << "Your earnings are now: " << (iscore=bet1 + iscore) << " Your goal is: " << goal << endl;
						if (iscore >= goal)
			{
				cout << "Congratulations! You have hit it big in Vegas and have reached your goal!" << endl;
				return 0;
			}
			}
			if (number < cpu)
			{
				cout << "You lose." << endl;
				cout << "Your earnings are now: " << (iscore=iscore - bet1) << " Your goal is: " << goal << endl;
			if (iscore < 0)
			{
				cout << "Your dreams of hitting it big in Vegas are shattered. Game Over." << endl;
				return 0;
			}
			}
			cout  << "Would you like to play again?\n(1)Yes\n(2)No" << endl;
			cin >> choice3;
			if (choice3 == 2)
			{
				cout << "Thanks for playing!" << endl;
				return 0;
			}
			if (choice3 == 1)
			
			{
				int value=1;
			}
		}
	}
	while (value=1)
	{
			srand(time(NULL));
	number=(rand()%21+1);
	number2=(rand()%10+2);
	cpu=(rand()%21+1);
			cout << "Enter your bet:" << endl;
		cin >> bet1;
		while (bet1 >= goal)
		{
				cout << "It's not wise to bet your goal or to bet over it!\nEnter your bet:" << endl;
		cin >> bet1;
		}
		cout << "Your total is: " << number  << endl;
		cout << "Earnings: " << iscore << " Goal: " << goal << "\n(1)Hit\n(2)Stay" << endl;
		cin >> choice2;
		if (choice2 == 1)
		{
			(total = number + number2);
			cout << "Your new total is: " << total << endl;
			cout << "Your opponent's total is: " << cpu << endl;
			if (total > 21)
			{		
		                 value2 = 1;
			}
				if (total == cpu)
			{
			cout << "It's a tie!" << endl;
			cout << "Your earnings are still: " << iscore << " Your goal is: " << goal << endl;
			}
			if (number > cpu)
			{
				cout << "You win!" << endl;
				cout << "Your earnings are now: " << (iscore =bet1 + iscore) << " Your goal is: " << goal << endl;
						if (iscore >= goal)
			{
				cout << "Congratulations! You have hit it big in Vegas and have reached your goal!" << endl;
				return 0;
			}
			
			}
			if (value2=1 and number > cpu)
			{
				cout << "JUST KIDDING! You went over 21. You lose." << endl;
						cout << "Your earnings are now: " << (iscore=iscore - bet1) << " Your goal is: " << goal << endl;
			}
			if (number < cpu)
			{
				cout << "You lose." << endl;
				cout << "Your earnings are now: " << (iscore=iscore - bet1) << " Your goal is: " << goal << endl;
			
			if (iscore < 0)
			{
				cout << "Your dreams of hitting it big in Vegas are shattered. Game Over." << endl;
				return 0;
			}
			}
				cout  << "Would you like to play again?\n(1)Yes\n(2)No" << endl;
			cin >> choice3;
			if (choice3 == 2)
			{
				cout << "Thanks for playing!" << endl;
				return 0;
			}
			if (choice3 == 1)
			{
				int value=1;
			}
		}
		if (choice2 == 2)
		{
			cout << "Your total is still: " << number << endl;
			cout << "Your opponent's total is: " << cpu << endl;
			if (number == cpu)
			{
				cout << "Its a tie!" << endl;
					cout << "Your earnings are still: " << iscore << " Your goal is: " << goal << endl;
				}
			if (number > cpu)
			{
				cout << "You win!" << endl;
				cout << "Your earnings are now: " << (iscore=bet1 + iscore) << " Your goal is: " << goal << endl;
						if (iscore >= goal)
			{
				cout << "Congratulations! You have hit it big in Vegas and have reached your goal!" << endl;
				return 0;
			}
			}
			if (number < cpu)
			{
				cout << "You lose." << endl;
				cout << "Your earnings are now: " << (iscore=iscore - bet1) << " Your goal is: " << goal << endl;
			if (iscore < 0)
			{
				cout << "Your dreams of hitting it big in Vegas are shattered. Game Over." << endl;
				return 0;
			}
			}
			cout  << "Would you like to play again?\n(1)Yes\n(2)No" << endl;
			cin >> choice3;
			if (choice3 == 2)
			{
				cout << "Thanks for playing!" << endl;
				return 0;
			}
			if (choice3 == 1)
			
			{
				int value=1;
		}
	}
}
	return 0;
}
Last edited on
I'm a noob myself, started around 3-4 days ago, but I think on if (choice2 == 1)

The number > cpu and number < cpu should be total > cpu and total < cpu

I'd also add
1
2
3
4
5
while (goal < 500)
{
cout << "enter a value higher than 500" << endl;
cin >> goal;
}

in the beginning
Last edited on
Topic archived. No new replies allowed.