Almost Done, but where am i going wrong...

Ive written a blackjack program for a term final. its about 200 lines and im all but done. im having 1 issue though and thats that i cant get my switch in my function called addcards to go off, and thereby add the cards. as such im getting my total == 999 b/c it wont add what the actual total should be. i know my switch has got to be a cause of the comotion so to speak.

Ill post the code, and if anyone could help me out in figuring this part out, id be truly appreciative.

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
// BlackJack.cpp : Defines the entry point for the console application.
//



#include <stdafx.h>
#include <cstdlib> //needed for rand() and srand()
#include <iostream>
#include <string.h>
#include <ctime>
#include <cmath>
using namespace std;

char cards[52]= { '2','2','2','2','3','3','3','3','4','4','4','4','5','5','5','5', 
'6','6','6','6','7','7','7','7','8','8','8','8','9','9','9','9','T','T','T','T','J','J','J','J',
'Q','Q','Q','Q','K','K','K','K','A','A','A','A' }; 

char Dealer[11]; 
char Player1[11];
int Player1total=0;
int Dealertotal=0;
int addcards(char count[11], int length)
    {

	cout<< "count[0] is: " << count[0] <<endl;
	cout<< "count[1] is: " << count[1] <<endl;
	cout<< "passed length is: " << length <<endl;
    int total=0;
    
       for (int x=1; x<length; x++)
       {
        switch (count[x])
		{
            case '2':
            total= total+2;
            break;
            case '3':
            total=total+3;
            break;
            case '4':
            total= total+4;
            break;
            case '5':
            total= total+5;
            break;
            case '6':
            total= total+6;
            break;
            case '7':
            total= total+7;
            break;
            case '8':
            total= total+8;
            break;
            case '9':
            total= total+9;
            break;
            case 'T':
            total= total+10;
            break;
            case 'J':
            total= total+10;
            break;
            case 'Q':
            total= total+10;
            break;
            case 'K':
            total= total+10;
            break;
            case 'A':
            total= total+11;
                if (total>22) {
                  total=total-10;
                }  // end of if statement.
                      
            break;
			default:
				total = 999;
				break;
        }  //end of switch statement
		cout<< "Player hand total is: " << total <<endl;
	   } // end of for statement.
     return (total);    
	}  //end of function.
            
                
            


int main() 

{
 
int cardsdelt=0;
int Dealerhandlength;
int Player1handlength;
int h;
int swap;
char temp;
char Hit;
char Stand;
char n;
int bust=0;

cout<<"Welcome to Justin's BlackJack table.  Ill be your dealer!" <<endl;
cout<< "We will be playing 1 hand at a time with one deck of cards.  I hope you will enjoy your stay. " <<endl;
srand (time(NULL)); 
	 for (int j=0; j<52;j++)
        {
        swap = rand()%52;
        temp = cards[j];
        cards[j] = cards[swap];
        cards[swap] = temp;
        }


	
	for (int h=0; h<2; h++)

	{
		Player1[h] = cards[cardsdelt];
        cardsdelt++;
		Dealer[h] = cards[cardsdelt];
        cardsdelt++;
				}
                
    
                
                
	Player1handlength =2;
    Dealerhandlength =2;

	Player1total = addcards((&Player1)[Player1handlength], Player1handlength);
    Dealertotal = addcards(Dealer, Dealerhandlength);
 

	cout<< "Players Hand is: " << Player1total << endl;
	cout<< "Dealers Hand is: " << Dealer[1] << endl;


	n='h';
	while (n=='h' || n== 'H'){
		cout << "Player 1 would you like to Hit, or Stand? ";
		cin>>n;

		switch(n)
		{
		case 'h':
		case 'H':
			Player1[Player1handlength++] = cards[cardsdelt++];  // puts next card from deck to hand
				cout << "your new card is a: " << Player1[Player1handlength - 1]<<endl;
				Player1total = addcards((&Player1)[Player1handlength], Player1handlength);
				if (Player1total>21) 
				{
					n='s';
					cout<< "PLAYER BUST!" <<endl;
					bust=1;
                break;
		case 'S':
		case 's':
			cout << "your final hand is: " << Player1total <<endl;
			break;
		}  
	} //end while

	while ((Dealertotal>18) && (bust == 0) {
		Dealer[Dealerhandlength++] = cards[cardsdelt++];  // puts a new card in dealers hand
		cout<< "Dealers new car is a: " <<Dealer[Dealerhandlength -1]<<endl;
		Dealertotal = addcards((&Dealer)[Dealerhandlength], Dealerhandlength);
	}
	Dealertotal = addcards((&Dealer)[Dealerhandlength], Dealerhandlength);
	cout<< "Dealer's total is: " << Dealertotal << endl;
	if (Dealertotal>21)
	{
		cout<< "The Dealer BUST.  Player WINS!!!!!" <<endl;
	}

	if (Player1total>Dealertotal) {
		cout<< "Player 1 WINS!!!!!!" << endl;
	}
	else {
		cout<< "Sorry but the Dealer has won! "<< endl;
	}

}
It looks like you are calling addcards() with the wrong first parameter sometimes.

For example, one line 133 you appear to be calling it with a pointer to the first unused element
in the Player1 array.

You also appear to be doing that on lines 169 and 171.

Because neither array is completely initialized, addcards() in these cases is essentially looking at
uninitialized memory, which is very likely not going to contain one of the card values your
switch looks for, hence you'll hit the default case.

On another note, you can use std::random_shuffle() to shuffle the cards as opposed to your
for() loop. std::random_shuffle( &cards[0], &cards[52] ); (defined in <algorithm>).

EDIT: And line 152.
Last edited on
thansk so much for the info J. I cant say i undertstand it, but im going to share it with a friend or 2 and see if they can help me understand what it is im doing wrong. hopefully that will clear it all up.

thanks again
Justin
ok guys, sorry for posting this silly thing again, but i am making headway and i feel good about that. heres whats goin on. essentially, its taking the 2 cards each person gets, but its only giving the value of 1 of the cards as opposed to the value of both cards added together.

after that, if the player hits, its dealing the player a whole new hand and i cant figure that out either. i can turn it in as is, but id like to get it all the way done.

thanks again all, the help has been greatly appreciated.

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
// BlackJack.cpp : Defines the entry point for the console application.
//



#include <stdafx.h>
#include <cstdlib> //needed for rand() and srand()
#include <iostream>
#include <string.h>
#include <ctime>
#include <cmath>
using namespace std;

char cards[52]= { '2','2','2','2','3','3','3','3','4','4','4','4','5','5','5','5', 
'6','6','6','6','7','7','7','7','8','8','8','8','9','9','9','9','T','T','T','T','J','J','J','J',
'Q','Q','Q','Q','K','K','K','K','A','A','A','A' }; 

char Dealer[11]; 
char Player1[11];
int Player1total=0;
int Dealertotal=0;
int addcards(char count[11], int length)
    {

	cout<< "count[0] is: " << count[0] <<endl;
	cout<< "count[1] is: " << count[1] <<endl;
	//cout<< "passed length is: " << length <<endl;
    
	int total=0;
    
       for (int x=1; x<length; x++)
       {
        switch (count[x])
		{
            case '2':
            total= total+2;
            break;
            case '3':
            total=total+3;
            break;
            case '4':
            total= total+4;
            break;
            case '5':
            total= total+5;
            break;
            case '6':
            total= total+6;
            break;
            case '7':
            total= total+7;
            break;
            case '8':
            total= total+8;
            break;
            case '9':
            total= total+9;
            break;
            case 'T':
            total= total+10;
            break;
            case 'J':
            total= total+10;
            break;
            case 'Q':
            total= total+10;
            break;
            case 'K':
            total= total+10;
            break;
            case 'A':
            total= total+11;
                if (total>22) {
                  total=total-10;
                }  // end of if statement.
                      
            break;
			default:
				total = 999;
				break;
        }  //end of switch statement
																
	   } // end of for statement.
     return (total);    
	}  //end of function.
            
                
            


int main() 

{
 
int cardsdelt=0;
int Dealerhandlength;
int Player1handlength;
int swap;
char temp;
char n;
int bust=0;

cout<<"Welcome to Justin's BlackJack table.  Ill be your dealer!" <<endl;
cout<< "We will be playing 1 hand at a time with one deck of cards.  I hope you will enjoy your stay. " <<endl;
srand (time(NULL)); 
	 for (int j=0; j<52;j++)
        {
        swap = rand()%52;
        temp = cards[j];
        cards[j] = cards[swap];
        cards[swap] = temp;
        }


	
	for (int h=0; h<2; h++)

	{
		Player1[h] = cards[cardsdelt];
        cardsdelt++;
		Dealer[h] = cards[cardsdelt];
        cardsdelt++;
				}
                
    
                
                
	Player1handlength =2;
    Dealerhandlength =2;

	Player1total = addcards(Player1, Player1handlength);
    Dealertotal = addcards(Dealer, Dealerhandlength);
 

	cout<< "Players Hand is: " << Player1total << endl;
	cout<< "Dealers Hand is: " << Dealer[1] << endl;


	n='h';
	while (n=='h' || n== 'H'){
		cout << "Player 1 would you like to Hit, or Stand? ";
		cin>>n;

		switch(n)
		{
		case 'h':
		case 'H':
			Player1[Player1handlength++] = cards[cardsdelt++];  // puts next card from deck to hand
				cout << "your new card is a: " << Player1[Player1handlength - 1]<<endl;
				Player1total = addcards(Player1, Player1handlength);
				if (Player1total>21) 
				{
					n='s';
					cout<< "PLAYER BUST!" <<endl;
					bust=1;
                break;
		case 'S':
		case 's':
			cout << "your final hand is: " << Player1total <<endl;
			break;
		}  
	} //end while

	while ((Dealertotal>18) && (bust == 0)) 
	{
		Dealer[Dealerhandlength++] = cards[cardsdelt++];  // puts a new card in dealers hand
		cout<< "Dealers new car is a: " <<Dealer[Dealerhandlength -1]<<endl;
		Dealertotal = addcards(Dealer, Dealerhandlength);
	}
	Dealertotal = addcards(Dealer, Dealerhandlength);
	cout<< "Dealer's total is: " << Dealertotal << endl;
	if (Dealertotal>21)
	{
		cout<< "The Dealer BUST.  Player WINS!!!!!" <<endl;
	}

	if (Player1total>Dealertotal) {
		cout<< "Player 1 WINS!!!!!!" << endl;
	}
	else {
		cout<< "Sorry but the Dealer has won! "<< endl;
	}

} 
}
Whoa, I just realized...

the brace on line 152... I believe you want to close that brace between lines 155 and 156, but
in reality it is being closed on 161, and the switch on 162, and the first while loop on 184.
Topic archived. No new replies allowed.