Can you help me fix this bug.

So I wrote this little blackjack program pretty simple and it was working fine except for some reason the draw() function had to be declared in the dealerPlay function so I did that it worked then I added a pause cin.get(); to make it so the person can actually see the text and it glitched out only giving 0s when you hit and when you stayed it hit anyways so I just removed the pause but the problem persist can someone help me.

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
#include <iostream>
#include <time.h>
#include <stdlib.h>

using namespace std;

int card1, card2, card3, card4, card5, card6, card7, dealerCard1, dealerCard2, dealerCard3, dealerCard4, dealerCard5, dealerCard6, dealerCard7;
int cardtotal, dealerTotal;
string hitstick;

int main()
{
    void dealCards();
    void dealThirdCard();
    void dealFourthCard();
    void dealFifthCard();
    void dealSixCard();
    void dealSevenCard();
    void bust();
    void winner();
    void dealerPlay();
    void draw();

    cout << "BlackJack - Made by Algore" << endl;
    cout << "---------------------------" << endl;
    cout << endl;
    cout << "Welcome to the game" << endl;
    cout << "press ENTER to start" << endl;
    cin.get();
    dealCards();
    // First 2 cards
    cout << "Your Cards" << endl << card1 << endl << card2 << endl;
        cardtotal = card1 + card2;
    cout << "Card Total: " << cardtotal << endl;
    cin.get();
        if (cardtotal > 21) {
            bust();
        }
        if (cardtotal == 21) {
            winner();
        }
    cout << "'hit' or 'stay'" << endl;
    cin >> hitstick;
        if (hitstick=="hit") {
            dealThirdCard();
        }else {
            dealerPlay();
        }
        system("cls");
        // third card
    cout << "BlackJack - Made by Algore" << endl;
    cout << "---------------------------" << endl;
    cout << endl;
    cout << "Here are your cards" << endl;
    cout << card1 << endl << card2 << endl << card3 << endl;
        cardtotal = cardtotal + card3;
    cout << "Your card total is: " << cardtotal << endl;
    cin.get();
        if (cardtotal > 21) {
            bust();
        }
        if (cardtotal == 21) {
            winner();
        }
    cout << "'hit' or  'stay'" << endl;
    cin >> hitstick;
        if (hitstick=="hit") {
            dealFourthCard();
        }else {
            dealerPlay();
        }
    system("cls");
    // fourth card
    cout << "BlackJack - Made by Algore" << endl;
    cout << "---------------------------" << endl;
    cout << endl;
    cout << "Your Cards" << endl;
    cout << card1 << endl << card2 << endl << card3 << endl << card4 << endl;
        cardtotal = cardtotal + card4;
    cout << "Your card total is: " << cardtotal << endl;
        if (cardtotal > 21) {
            bust();
        }
        if (cardtotal == 21) {
            winner();
        }
            cout << "'hit' or  'stay'" << endl;
    cin >> hitstick;
        if (hitstick=="hit") {
            dealFifthCard();
        }else {
            dealerPlay();
        }
      system("cls");
      // fifth card
    cout << "BlackJack - Made by Algore" << endl;
    cout << "---------------------------" << endl;
    cout << endl;
    cout << "Your Cards" << endl;
    cout << card1 << endl << card2 << endl << card3 << endl << card4 << endl << card5 << endl;
        cardtotal = cardtotal + card5;
    cout << "Your card total is: " << cardtotal << endl;
        if (cardtotal > 21) {
                if (cardtotal > dealerTotal) {
                    winner();
                }
            bust();
        }
        if (cardtotal == 21) {
            winner();
        }
            cout << "'hit' or  'stay'" << endl;
    cin >> hitstick;
        if (hitstick=="hit") {
            dealSixCard();
        }else {
            dealerPlay();
        }

      system("cls");
    cout << "BlackJack - Made by Algore" << endl;
    cout << "---------------------------" << endl;
    cout << endl;
    cout << "Your Cards" << endl;
    cout << card1 << endl << card2 << endl << card3 << endl << card4 << endl << card5 << endl << card6 << endl;
        cardtotal = cardtotal + card6;
    cout << "Your card total is: " << cardtotal << endl;
        if (cardtotal > 21) {
            bust();
        }
        if (cardtotal == 21) {
            winner();
        }
        dealerPlay();
        cin.get();
        return 0;
}

void dealCards()  {

    srand(time(NULL));
    card1 = rand() % 11 + 1;
    card2 = rand() % 11 + 1;
    return;
}

void dealThirdCard() {
    srand(time(NULL));
    card3 = rand() % 11 + 1;
}

void dealFourthCard() {
    srand(time(NULL));
    card4 = rand() % 11 + 1;
}
void dealFifthCard() {
    srand(time(NULL));
    card5 = rand() % 11 + 1;
}
void dealSixCard() {
    srand(time(NULL));
    card6 = rand() % 11 + 1;
}
void dealSevenCard() {
    srand(time(NULL));
    card7 = rand() % 11 + 1;
}


void bust() {
    system("cls");
    cout << "BlackJack - Made by Algore" << endl;
    cout << "---------------------------" << endl << endl;
    cout << "BUST\n";
    cout << "Play Again? y/n \n";
    string sel;
    cin >> sel;
    if (sel == "y") {
        main();
    } else {cin.get();}
}

void winner() {
    system("cls");
    cout << "BlackJack - Made by Algore" << endl;
    cout << "---------------------------" << endl << endl;
    cout << "Congratulations you have won\n";
    return;
}

void dealerPlay() {
    void draw();
    system("cls");
    cout << "Your Card Total: " << cardtotal << endl;
    cout << "Dealer Draws\n\n\n";
    srand(time(NULL));
    dealerCard1 = rand() % 11 + 1;
    dealerCard2 = rand() % 11 + 1;
        dealerTotal = dealerCard1 + dealerCard2;
    if (dealerTotal >= 17) {
        draw();
    } else {
    srand(time(NULL));
    dealerCard3 = rand() % 11 + 1;
    }
    if (dealerTotal >= 17) {
        draw();
    } else {
    srand(time(NULL));
    dealerCard4 = rand() % 11 + 1;
    }
    if (dealerTotal >= 17) {
        draw();
    } else {
    srand(time(NULL));
    dealerCard5 = rand() % 11 + 1;
    }
    if (dealerTotal >= 17) {
        draw();
    } else {
    srand(time(NULL));
    dealerCard6 = rand() % 11 + 1;
    }
}

void draw() {
cout << "Dealer Total: " << dealerTotal;
if (dealerTotal == 21) {
    bust();
} else if (dealerTotal > cardtotal) {
    bust();
} else {
    winner();
}
}


Any help would be appericated
bump
rather than
card1, card2, card3
why don't you use an array or vector instead ?
also...system is a bad practice
@Flaze07 if i do that it would cause other issues and i am aware using system is a bad practive but i am unaware of any other way to clear the screen other than system("cls");
both winders and unix have console drawing library that has things like moving the cursor to a location, clearing the screen, colored fonts, and other such things. I can't recall the libraries, but they are out there.

You can also clear the screen by writing X blank lines to it, though this leaves the cursor at the bottom instead of the top.

And system is just fine so long as you understand the issue. There are replacements for it you can use later, like shellexecute family, or spawn, etc. Your game isn't likely to become a major threat to your machine's security for calling cls :)



Last edited on
there's an article that @duoas made that can clear screen
What rules do you use?
1
2
3
4
if (cardtotal == 21) 
{
    winner();
}

The dealer still could get 21 as well and then it's a draw.

1
2
3
4
if (dealerTotal >= 17) 
{
   draw();
}

Normally the dealer has to draw while < 17. With >= 17 he hardly has a chance.
@Thomas my friends mom is a black jack dealer at a casino they draw until they're card is great than or equal to 17 then they no longer draw also if you draw at 17 you will more than likely bust anyways
Topic archived. No new replies allowed.