Case Statement

I am writing a simple blackjack program and I am wondering can you use arrays in switch statement? (switch (ranks[r])) I am trying to assign each card a value so I can get the sum of the card in the game.

Thank you

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
#include <cstdlib>
#include <iostream>
#include <time.h>
#include <math.h>
using namespace std;
string suits[4] = {"Hearts", "Diamonds", "Spades", "Clubs"};
string ranks[13] = {"Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"};
int card_drawn[52];

int cards_remaining = 52;

//This function finds the Nth element of card_drawn and skipping over all those those elements already set to true.
int select_next_available(int n) {
    int i = 0;
    //at the beginning of deck, skip past cards already drawn.
    while (card_drawn[i])
    i++;
    while (n-- > 0) { //do the following n times:
    i++; //advanced next card
    while (card_drawn[i]) //skip past cards
          i++;// already drawn.
    }//end while
    card_drawn[i] = true; // note card to be drawn
    return i; //return this number
} //end select_next_available function

int rand_0toN1(int n) {
return rand() % n;
} //end function

void draw_a_card(int &r, int &s, int &t){// int &n, int &card
int n, card, counter;
n = rand_0toN1(cards_remaining--); //get random number from the remaining card availble
card = select_next_available(n);
r = card % 13; //random index (0 to 12) into rank array
s = card % 4; //random index (0 to 3) into suits array
cout << ranks[r] << " of " << suits[s] << endl;
switch (ranks[r])
      {
         case "Ace":
            counter = 1 + counter;
            break;
         case "Two":
            counter = 2 + counter;
            break;
         case "Three":
            counter = 3 + counter;
            break;
      }
cout << counter << endl;

} //end draw a card

int main ()
{
int n, c, i;
char a;
// n == 2; This is a comparison operator ==
n = 2;
c = 1;
srand(time(NULL));
int card, type;

cout << "Blackjack" << endl;
cout << "Play (Y/N)" << endl;
cin >> a;
if (a == 'y' || a == 'Y');
for(i = 0; i < n; i++) // 0, 1, break gives you two iterations
draw_a_card(card,type);
//counter

while (1)
{
cout << "Hit?" << endl;
cin >> a;
if (a == 'y' || a == 'Y');
for (i = 0; i < c; i++)
draw_a_card(card,type);
//counter

}
system("PAUSE");
return 0;
}//end main 
Last edited on
You can't use a switch that way. Sorry.
Do you have any other way that I can get the sum of the card you currently have?
Use an if/else chain instead of a switch.
Topic archived. No new replies allowed.