Blackjack HELP

Hey there i've a problem. I want to have 2 cards at the beginning but more cards come out when either J Q K appears. Please help me. Thanks

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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#include <iostream>
#include <stdio.h>
#include <cstdlib>
#include <time.h>
 
using namespace std;
void choiceD(int *card, int *card2);
void choiceP(int *card, int *card2);
int computecard(const int *card);
void showcardD(const int *card);
void showcard(const int *card);
void generatecard(int *card);
void comparecard(int *playerC, int *dealerC);
 
 
void filtercards(int *card)
{
    int i,sum;
 
    for(i=0;i<5;++i)
    {
      if(*(card+i)==11)
        {
            cout<<"J"<<" ";
            *(card+i)=10;
 
      }
    else if((*card+i)==12)
        {
            cout<<"Q"<<" ";
            *(card+i)=10;
 
            }
    else if((*card+i)==13)
        {
            cout<<"K"<<" ";
            *(card+i)=10;
 
            }
    else if(1<(*card+i)<=10)
        {
            continue;
    }
    else if(*(card+i)==0)
        {break;}
    else if(*(card+i)==1)
    {
        sum=computecard(card);
        if(sum<=10)
        {
            *(card+i)=11;
        }
        else if(10<sum<22)
            *(card+i)=1;
    }
 
    }
 
 
 
}
 
void showcardD(const int *card)
{
   int i,sum=0;
   cout<<"Dealer's cards: ";
 
   for(i=0;i<5;++i)
   {
       if(*(card+i)==0)
           break;
       else
           cout <<*(card+i)<<" ";
 
   }
   cout <<endl;
   sum=computecard(card);
   cout<<"Dealer's Value: "<<sum;
   cout<<endl<<"*****************************************************"<< endl;
   if(sum>21)
   {
      cout<<"You Win!";
      exit(0);
   }
   else if(sum==21)
   {
       cout<<"You Lose";
       exit(0);
   }
}
 
void showcard(const int *card)
{
   int i,sum=0;
   cout<<"Your cards: ";
 
   for(i=0;i<5;++i)
   {
       if(*(card+i)==0)
           break;
       else
           cout <<*(card+i)<<" ";
 
   }
   cout <<endl;
   sum=computecard(card);
   cout<<"Your Value: "<<sum;
   if(sum>21)
   {
      cout<<"You Lose!";
      exit(0);
   }
   else if(sum==21)
   {
       cout<<"You Win";
       exit(0);
   }
 
 
}
int computecard(const int *card)
{
   int i,sum=0;
   for(i=0;i<5;i++)
   {
       sum += *(card+i);
   }
   return sum;
}
void generatecard(int *card)
{
   int i;
   for(i=0;i<2;i++)
   {
       *(card+i)=rand()%13+1;
       filtercards(card);
       cout <<*(card+i)<<" ";
   }
   cout<<endl;
 
}
 
void comparecard(int *playerC, int *dealerC)
{
   int sumP, sumD;
   sumP=computecard(playerC);
   sumD=computecard(dealerC);
   if(sumD>sumP)
   {
     cout<<"You Lose";
       exit(0);
   }
 
   else if(sumP>sumD)
   {
       cout<<"You Win";
       exit(0);
   }
 
   else
   {
       cout<<"You Lose";
       exit(0);
   }
 
}
 
void choiceP(int *card, int *card2)
{
   int i,action;
   repeat:
       cout << "*****************************************************" << endl;
       cout << "1) Hit"<<endl;
       cout << "2) Stand"<< endl;
       cout << "What's your action: ";
       cin>> action;
   switch(action)
   {
 
 
 
       case 1:
           {
               for(i=0;i<5;++i)
               {
                   if(*(card+i)==0)
                   {
                       *(card+i)=rand()%13+1;
                       filtercards(card);
                       break;
                   }
                   else
                       continue;
 
               }
               cout<<endl<<"*****************************************************"<< endl;
               showcard(card);
               cout<<endl;
 
               choiceP(card, card2);
               break;
 
 
           }
       case 2:
           {
               cout<<"Player stands."<<endl;
 
               choiceD(card2, card);
               break;
 
           }
       default:
           {
               cout<<"Please choose again."<<endl;
 
               goto repeat;
           }
   }
}
 
void choiceD(int *card, int *card2)
{
 
   int i,action;
   repeat:
       cout << "*****************************************************" << endl;
       cout << "1) Hit"<<endl;
       cout << "2) Stop"<< endl;
       cout << "What's your action (Dealer): ";
       cin>> action;
   switch(action)
   {
 
 
       case 1:
           {
               for(i=0;i<5;++i)
               {
                   if(*(card+i)==0)
                   {
                       *(card+i)=rand()%13+1;
                       filtercards(card);
                       break;
                   }
                   else
                       continue;
 
               }
               cout<<endl<<"*****************************************************"<< endl;
 
               showcardD(card);
               cout<<endl;
               goto repeat;
 
               break;
 
 
 
           }
       case 2:
           {
               cout<<"Dealer Passes."<<endl;
               cout<<endl<<"*****************************************************"<< endl;
 
               comparecard(card2,card);
               break;
 
           }
       default:
           {
               cout<<"Please choose again."<<endl;
               goto repeat;
 
           }
   }
}
 
int main()
{
   srand(time(NULL));
   int playerC[5]={0}, playerV, dealerC[5]={0}, dealerV, action;
   cout << "=============Welcome to Blackjack=============" << endl;
   cout << "*****************************************************" << endl;
   cout << "Your Cards: ";
   generatecard(playerC);
   cout << "Your Value: " ;
   playerV=computecard(playerC);
   cout << playerV<<endl;
   cout << "*****************************************************" << endl;
   cout << "Dealer's Cards: ";
   generatecard(dealerC);
   cout << "Dealer's Value: ";
   dealerV=computecard(dealerC);
   cout << dealerV<<endl;
   choiceP(playerC, dealerC);
   cout<<"Dealer's Turn."<<endl;
   cout << "*****************************************************" << endl;
   choiceD(dealerC, playerC);
 
 
 
 
 
 
 
 
 
   return 0;
}
Okay, so before we address your question. A few very important things:

-You must not be using goto. I really cannot stress this enough. It makes code difficult to read and difficult to debug, which will be a pain for you in future (if not already) and is a pain for people on forums like this trying to help.

-It would be greatly helpful if you comment your code, for me, for you, for everyone.

-A smaller point. You do not need <stdio> and <ctime> is the proper version of the library to use, rather than <time.h>.

-It would also help greatly if you used consistent indentation.

General advice aside now, onto your question. . .

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
if(*(card+i)==11)
        {
            cout<<"J"<<" ";
            *(card+i)=10;
      }
    else if((*card+i)==12)
        {
            cout<<"Q"<<" ";
            *(card+i)=10;

            }
    else if((*card+i)==13)
        {
            cout<<"K"<<" ";
            *(card+i)=10;

            }


This part of your code displays the appropriate char if the card is J, Q or K, however...

1
2
3
*(card+i)=rand()%13+1;
       filtercards(card);
       cout <<*(card+i)<<" ";


This line following the function call spits out the number regardless of if J, Q or K was already displayed.

Also, looking back at this piece of code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
if(*(card+i)==11)
        {
            cout<<"J"<<" ";
            *(card+i)=10;
      }
    else if((*card+i)==12)
        {
            cout<<"Q"<<" ";
            *(card+i)=10;

            }
    else if((*card+i)==13)
        {
            cout<<"K"<<" ";
            *(card+i)=10;

            }


This is going through J, Q and K if you get a J and through Q and K if you get a queen.

Or basically, I don't think this method is working and it seems confusing as hell, so my suggestion is to use a different method to display your cards and work out the sums. One suggestion is storing cards in an array and converting 11,12 and 13 to 10 in any sum function and J,Q or K (as appropriate) in any display function.
@Mats

Thanks alot! Really Appreciate your help, it's my first time coding using C++ so im still not familiar with the headers and stuff. Thanks alot. I will look into that! :D
Topic archived. No new replies allowed.