Blackjack

When i run this code, it will sometimes create legitimate numbers, but usually creates really high numbers. Could someone tell me what i'm doing wrong?

Here's the code:
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
/*
  Name: BlackJack
  Copyright: 1.0
  Author: Riun23
  Date: 08/09/10 18:16
  Description: Blackjack, without Insurance
*/
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
using namespace std;

class dealer
{
      public:
             void Deal();
             int Hit();
             int GetCard();
             int Stand();
             int UpdateVals(int card);
             int checkFive();
      private:
              int numofcards;
              int total;
};

int dealer::Hit()
{
    int card;
    int sel;
    string suit;
    card = GetCard();
    
    numofcards = numofcards + 1;
    checkFive();
    sel = rand() % 5;
    switch (sel)
    {
           case 1:
                suit = "hearts";
                break;
           case 2:
                suit = "spades";
                break;
           case 3:
                suit = "diamonds";
           case 4:
                suit = "clubs";
                break;
    }
    cout << "It's a " << card << " of " << suit << ".\n";
    UpdateVals(card);
}
int dealer::GetCard()
{
    
    int tmp;
    int card;
    int foc;
    srand ( time(NULL) );
    foc = rand() % 3;
    switch (foc)
    {
           case 1:
                card = rand() % 11;
                break;
           case 2:
                tmp = rand() % 5;
                switch (tmp)
                {
                       case 1:
                            card = 10;
                            break;
                       case 2:
                            card = 10;
                            break;
                       case 3:
                            card = 10;
                       case 4:
                            card = 1;
                            break;
                }
                break;
    }
    return card;
}
int dealer::Stand()
{
        system("PAUSE");
}
int dealer::UpdateVals(int card)
{
    total = total + card;
}
int dealer::checkFive()
{
    if (numofcards >= 2)
    {
                   cout << "Five Card Charlie! You Win!";
    } 
}                
void dealer::Deal()
{
    int cardArr[2]; 
    int tmp;
    int card;
    int foc;
    srand ( time(NULL) );
    for (int i =0; i <= 1; i++)
    {
    foc = rand() % 3;
    switch (foc)
    {
           case 1:
                cardArr[i] = rand() % 11;
                break;
           case 2:
                tmp = rand() % 5;
                switch (tmp)
                {
                       case 1:
                            cardArr[i] = 10;
                            break;
                       case 2:
                            cardArr[i] = 10;
                            break;
                       case 3:
                            cardArr[i] = 10;
                       case 4:
                            cardArr[i] = 1;
                            break;
                }
                break;
    }
    }
    total = cardArr[0] + cardArr[1];
    cout << "Your total is: " << total << ".\n";
    if (total > 21)
    {
              cout << "You Busted!(You lose)\n";
    }      
}
int main()
{
    dealer Player1;
    Player1.Deal();
    system("PAUSE");
    return 0;
}     

Firstly, some (most) of the functions you made for your Dealer class have return types of int, but do not return a value. In some compilers that may work, but is generally a no no. If there's no reason to return a value, change the function return types to void.

Also, you need to initialize your variables.

1
2
3
4
5
6
7
8
9
10
11
12
int dealer::Hit()
{
    int card; //needs to be: int card = 0;
    int sel;   //needs to be: int sel = 0;
    string suit;
    card = GetCard();
    
    numofcards = numofcards + 1;
    checkFive();
    sel = rand() % 5;
    switch (sel)
...


Initializations like that should be made for every variable, because when your program allocates memory for it, it doesn't automatically assume 0. It's got whatever value is in the memory area already.


After applying that, the program compiles fine, and doesn't give me those wacky numbers.
The switch on line 67 does not have a case for zero so you are returning
an uninitialized value.

Topic archived. No new replies allowed.