BlackJack scoring.

Hey guys, i wrote a bit of code and im having trouble debugging it, any help would be 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
#include <iostream>

using namespace std;

int main(){

	int cards, i, total=0, aceCount=0, t, j, q, k, a;
  char temp;

  cout << "How many cards? (Between 2 and 5):" ;
  cin >> cards ;
  if (cards < 2 || cards > 5){
   
    cout << "Not a valid number of cards.";
    
  
  }

  else {
    cout << "Enter card values. (2-9, or 't', 'j', 'q', 'k', 'a'):";
    for (i=0; i<cards; i++){
      cin >> temp;
      switch (temp){
      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:
	case j:
	case q:
	case k:
	total = total + 10;
	break;
	case a:
      
	total = total + 11;
	aceCount = aceCount + 1;
	break;
      }
		
    }
cout << total<<endl;
 if (total < 21){

      cout << "Total is: " << total << endl;
    }

    else{
      if (aceCount!=0){
	total = total - 10;
	aceCount = aceCount - 1;
	if (total < 21){

	  cout << "Total is: " << total << endl;
	}
	  }
	}
  }
}


if you attempt to compile, it doesnt like the case variable name being a letter, not sure how i can go about solving that, and it appears to not be adding up the scores, it gives 0 as the total each time.
@Omar Alamy

Your main problem was in that you weren't checking for a char value in the switch section of code. You needed to put single quotes around each of the cases.
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
// Blackjack values.cpp : main project file.

#include <iostream>

using namespace std;

int main()
{

	int cards, i, total=0, aceCount=0; // t, j, q, k, a variables NOT being used yet. Add in when needed
	char temp;

	cout << "How many cards? (Between 2 and 5):" ;
	cin >> cards ;
	if (cards < 2 || cards > 5)
	{
		cout << "Not a valid number of cards.";
	}

	else
	{
		for (i=0; i<cards; i++)
		{
			cout << "Enter value of card " << i+1 << ". (2-9, or 't', 'j', 'q', 'k', 'a'):";
			cin >> temp;
			switch (temp)
			{
			case '2':
				total +=2;
				break;
			case '3':
				total +=3;
				break;
			case '4':
				total +=4;
				break;
			case '5':
				total +=5;
				break;
			case '6':
				total +=6;
				break;
			case '7':
				total +=7;
				break;
			case '8':
				total +=8;
				break;
			case '9':
				total +=9;
				break;

			case 't':
			case 'j':
			case 'q':
			case 'k':
				total +=10;
				break;
			case 'a':
				total +=11;
				aceCount++;
			}
		}
		cout << total<<endl;
		if (total <= 21)
		{
			cout << "Your total is: " << total << endl;
		}
		else if (aceCount!=0 && total > 21) // Only subtract 10 if total > 21
		{
			do
			{
			total-=10;
			aceCount--;
			} while (aceCount >0 && total > 21); // While still over 21 and an ace that equals 11
			                                     // have the ace equal one, and subtract 10 from total
			if (total <= 21) 
			{
				cout << "Total is: " << total << endl;
			}
		}
	}
}
Last edited on
thanks for the help!
if i wanted to make this a function, am i on the right path here? i want it to take the array that is the current hand, and calculate the score.

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
#include <iostream>
//#include "deck.h"
using namespace std;
const int SIZE=50;
int scoreDealer(char currenthand[], int numofelements);
int main()
{
	char currenthand[SIZE];
	int numofelements;
	cout << "Enter the number of cards in your hand" <<endl;
	cin >> numofelements;
	
	if (numofelements > 5 || numofelements < 2)
	{
	cout << "Please enter a number between 2 and 5" <<endl;
		}
		
	cout << "Enter values for your hand" << endl;
	scoreDealer(currenthand, numofelements);
	
		return 0;
}
int scoreDealer(char currenthand[], int numofelements)
{
int aceCount=0, total=0;

for (int i=0; i<numofelements;i++)
cin >> currenthand;
switch (currenthand[i])
{
				case '2':
					total +=2;
					break;
				case '3':
					total +=3;
					break;
				case '4':
					total +=4;
					break;
				case '5':
					total +=5;
					break;
				case '6':
					total +=6;
					break;
				case '7':
					total +=7;
					break;
				case '8':
					total +=8;
					break;
				case '9':
					total +=9;
					break;
					
				case 't':
				case 'j':
				case 'q':
				case 'k':
					total +=10;
					break;
				case 'a':
					total +=11;
					aceCount++;
					break;
					}}}


if (total <= 21)
		{
			return total;
		}
		else if (aceCount!=0 && total > 21) // Only subtract 10 if total > 21
		{
			do
			{
				total-=10;
				aceCount--;
			} while (aceCount >0 && total > 21); // While still over 21 and an ace that equals 11
			// have the ace equal one, and subtract 10 from total
			if (total <= 21) 
			{
				return total;
			}
		}
		}}
Not really. First you say
take the array that is the current hand, and calculate the score.
, but you don't get the values in the main section. You request it in the scoreDealer() function. So, passing currenthand[] doesn't help, as it's empty. Also, you have too many closing braces in the scoreDealer() function.

I'm not very well versed in the passing of arrays to functions, as I'm not comfortable with pointers, etc. Hopefully, someone else can point you through this.
line 28 should be cin >> currenthand[i];

I would say you're on the right path, though you haven't written the part where total is greater than 21 and there aren't any aces.

I think I would break it up even more. For example, one function just to calculate the value of the card:
1
2
3
4
5
6
7
8
9
10
11
12
13
int evaluate(char ch){
  switch (ch){
  case '1': return 1;
  case '2': return 2;
  ...
  //Maybe some default for an invalid card
}

//Then back in the main (or wherever)

total += evaluate(card);

// also have something here to know if the card is an ace 


Then to have something similar to adjust the total based one how many aces.





Last edited on
Topic archived. No new replies allowed.