Help with blackjack program?

Having trouble with a blackjack program, what is NOT PROGRAMMED YET is the dealer's cycle, and deciding the winner. I can do that on my own, the problem is on the players cycle when he hits it always says his cards are worth somewhere in the 3200s (absurd, i know) but on the first 2 draws it's totally accurate.

Another problem, sometimes there are just nonsensical cards like the 26 of spades, or the 1 of diamonds. I've tried fixing but i can't.

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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
  // Author: Seamus Imes
//Class: Computer Science 2
//Program: Blackjack

//Known Bugs:
//After the first 2 cards are dealt, when you draw another one it always output the value of the 3 cards as somewhere in the 3200's. No clue why.
//Occasionally it will say you have a nonexistent card, like the 26 of spades, or the 1 of hearts.

//What's left to do:
//Fix errors
//Program a dealer's turn where the dealer hits until he has atleast seventeen, and then stays.
//Calculate who wins and print he winner.
//After that it is finished.



#include <iostream>
#include <string>
#include <time.h>

using namespace std;

#define CLUBS 0
#define DIAMONDS 1
#define HEARTS 2
#define SPADES 3

#define NOCARD -1

struct card{
int suit;
int rank;
bool dealt;
bool softace;
};

//takes the deck and the card to print as arguments
void printCard(card deck[51], int mycard) {
string a;
string b;
bool facecard = true;
//convert the suit variable into a string
if (deck[mycard].suit == CLUBS) {
b = "Clubs";
}
else if (deck[mycard].suit == DIAMONDS) {
b = "Diamonds";
}
else if (deck[mycard].suit == HEARTS) {
b = "Hearts";
}
else {
b = "Spades";
}
//convert the rank variable into a string
if (deck[mycard].rank == 0) {
a = "Ace";
}

else if (deck[mycard].rank == 10) {
a = "Jack";
}
else if (deck[mycard].rank == 11) {
a = "Queen";
}
else if (deck[mycard].rank == 12) {
a = "King";
}
else {
a = deck[mycard].rank;
facecard = false;
}
if (facecard == true) {
cout << a << " of " << b;
}
else if (facecard == false) {
cout << deck[mycard].rank << " of " << b;
}
}

//function that gives the next free index of one of the hand arrays
int nextFree(int hand[10]) {
for (int i=0;i<11;i++){
if (hand[i] == NOCARD) {
return i;
}
}
return 0;
}

bool hasAce(int hand[10]) {
for(int i=0;i<10;i++){
if (hand[i] == 0||13||26||39) {
return true;
cout << "You have an ace" << endl;
}
}
return false;
}

int cardValue(card deck[51], int mycard){
int value;
if (deck[mycard].rank == 0) {
//if (deck[mycard].softace == true) {
// value = 1;
//}
//else {
         value = 11;
//}
        }

        else if (deck[mycard].rank == 10) {
                value = 10;
        }
        else if (deck[mycard].rank == 11) {
                value = 10;
        }
        else if (deck[mycard].rank == 12) {
                value = 10;
        }
        else {
                value = deck[mycard].rank;
        }	
return value;
}
int randCard(card deck[51]) {
int randnum = rand() % 52;
while (deck[randnum].dealt == 1) {
randnum = rand() % 52;
}
return randnum;
}


bool hasHard(card deck[51], int hand[10]) {
        for (int i=0;i<10;i++){
                if (deck[hand[i]].rank == 1 && deck[hand[i]].softace == false) {
                        return true;
                }
        }
        return false;
}


int makeSoft(card deck[51], int hand[10]) {
for (int i=0;i<10;i++){
if (deck[hand[i]].rank == 1 && deck[hand[i]].softace == false) {
deck[hand[i]].softace = true;
return 0;
}
}
return 0;
}

int main() {
int randcard;
srand(time(NULL));	
//intializes the dealer and player's hands arrays
int playervalue;
int dealervalue;
int playerhand[10];
int dealerhand[10];
for (int i=0;i<11;i++){
playerhand[i] = NOCARD;
dealerhand[i] = NOCARD;
}

//populate the deck
card deck[51];	
for (int i = 0; i < 4; i++){
int mysuit = i;
for (int i = 0; i < 13; i++) {
deck[i+mysuit*13].suit = mysuit;
deck[i+mysuit*13].rank = i;
deck[i+mysuit*13].dealt = false;
deck[i+mysuit*13].softace = false;
}
}
// deal cards to both the player and the dealer
for (int i=0;i<2;i++) {
//player
randcard = randCard(deck);
deck[randcard].dealt = 1;
playerhand[i] = randcard;
//dealer
randcard = randCard(deck);
deck[randcard].dealt = 1;
dealerhand[i] = randcard;	
}	

//Start and your first draw text
cout << "Welcome to Blackjack!" << endl << endl;
cout << "You drew two cards." << endl << "The ";
printCard(deck, playerhand[0]);
cout << " and the ";
printCard(deck, playerhand[1]);
cout << "." << endl;
cout << "Value: " << cardValue(deck, playerhand[0])+cardValue(deck, playerhand[1]) << "." << endl << endl;

//dealers first draw text
cout << "The dealer drew two cards." << endl;
cout << "The non-hole card is the ";
printCard(deck, dealerhand[0]);
cout << "." << endl << "Value: " << cardValue(deck, dealerhand[0]) << endl;


//check if either the player or the dealer got a blackjack

//player gets blackjack
if (cardValue(deck, playerhand[0])+cardValue(deck, playerhand[1]) == 21) {
if (cardValue(deck, dealerhand[0])+cardValue(deck, dealerhand[1]) != 21) {
cout << "You won on the first round! Congratulations.";
return 0;
}
}

//both get blackjack
if (cardValue(deck, playerhand[0])+cardValue(deck, playerhand[1]) == 21) {
if (cardValue(deck, dealerhand[0])+cardValue(deck, dealerhand[1]) == 21) {
cout << "You tied with the dealer, better luck next time.";
return 0;
}
}
//dealer gets blackjack
if (cardValue(deck, playerhand[0])+cardValue(deck, playerhand[1]) != 21) {
if (cardValue(deck, dealerhand[0])+cardValue(deck, dealerhand[1]) == 21) {
cout << "How unfortunate! You've lost on the first round. Better luck next time." << endl;
return 0;
}
}

//The point at which all the starting conditions are set and the real "game" can begin, that is, the infinite game loop.
bool playing = true;

while(playing == true) {
//ask player if he wants to hit or stay
string txtinput;
cout << "Would you like to hit? Y/N:";
cin >> txtinput;
if (txtinput == "Y") {
cout << "You hit.";
randcard = randCard(deck);
playerhand[nextFree(playerhand)] = randcard;
          deck[randcard].dealt = 1;
}
if (txtinput == "N") {
cout << "You stayed.";
playing = false;
break;
}

cout << endl << "You have " << nextFree(playerhand) << " cards." << endl;
for (int i = 0; i < nextFree(playerhand); i++) {
printCard(deck, playerhand[i]);
cout << endl;
}
for (int i = 0; i < nextFree(playerhand); i++) {

playervalue = playervalue + cardValue(deck, playerhand[i]);
}
cout << "Value: " << playervalue << endl;
if (playervalue > 21) {
if (hasAce(playerhand) && hasHard(deck, playerhand)){
makeSoft(deck, playerhand);
cout << "You now count your ace as 1, instead of 11." << endl;
}
else {cout << "You lost! Better luck next time.";
return 0;
}
}
}



        

// ---------------------TESTS-----------------------
/* // this tests whether the deck populates correctly
for (int i = 0;i < 52;i++){
cout << i;
cout << "Rank: " << deck[i].rank;
cout << " Suit: " << deck[i].suit;
cout << " Dealt: " << deck[i].dealt << endl;
}*/

//cout << deck[playerhand[0]].suit << endl;
return 0;
};
Bump!
bump! sorry i dont know how to format thats how it ended up when i pasted.
Edit your first post with nice formatting. It's horrible to try to read that atm.
Topic archived. No new replies allowed.