This code Associates each number with cards, so 1 = ace of hearts/spades/diamonds/clubs, 2 = 2 of clubs/spades..... and so on so here is the code

``````void DeckToCards(int Deck[]){

//Sets each element into Cards;
for(int i=0;i<52;i++)
{
switch(Deck[i]){

case 1:{ if(Deck[i]==1 && i<14)//Clubs
char Aces_C[]= " A (of clubs)";
else if(Deck[i]==1 && i>13 && i<27)//Spades
char Aces_S[]= " A (of spades) ";
else if(Deck[i] && i>26 && i<40)//Hearts
char Aces_H[]=" A (of hearts) ";
else if(i>=40)//Diamonds
char Aces_D[]=" A (of diamonds) ";
}

case 2:{	if(Deck[i]==2 && i<14) // 2 of Clubs
char 2_C[] = " 2 (of clubs";
else if(Deck[i]==2 && i>13 && i<27)// 2 of Spades
char 2_S[]= " 2 ( of spades)";
else if(Deck[i]==2 && i>26 && i<40)//Hearts
char 2_H[]= " 2 (of hearts)";
else if(i>=40)//Diamonds
char 2_D[]=" 2 (of diamonds)

}
case 3:

}
}``````

Is there any other smarter way of going about this. This code is part of a blackjack game by the way.

## All 4 Replies

1) The case statements need at break; statement at the end of the case to prevent other cases from being executed

``````case 1:
...
break;
case 2:
...
break;
// etc``````

2) You don't need to check the value of Deck within the case statements because that is already done in the switch statement.

3) You can't declare character arrays within if statements like that because they will disappear (go out of scope) as soon as the if statement terminates. Declare character arrays outside the switch statement so that they are in scope for the entire function.

1) The case statements need at break; statement at the end of the case to prevent other cases from being executed

``````case 1:
...
break;
case 2:
...
break;
// etc``````

2) You don't need to check the value of Deck within the case statements because that is already done in the switch statement.

3) You can't declare character arrays within if statements like that because they will disappear (go out of scope) as soon as the if statement terminates. Declare character arrays outside the switch statement so that they are in scope for the entire function.

Thanks I get that but, is there another way this can be executed, without doing this long and tedious algorithm?

you could use <map> to map the loop counter i with a string. A simple example, uses an array of maps to represent the 4 kinds of cards.

``````#include <iostream>
#include <string>
#include <map>
using namespace std;

map<int, string> deck[4];
int main()
{
deck[0][1] = "Ace of Spaces";
deck[1][1] = "Ace of Clubs";
deck[2][1] = "Ace of Diamonds";
deck[3][1] = "Ace of Hearts";

string kind = deck[2][1];
cout << kind << "\n";
}``````
commented: Even though I haven't learned this, this post is helpful because you made it clear of the use of <map> . Thank YOU +1

or you could make a card class, e.g.

``````#include <iostream>
#include <string>
using namespace std;
class Card{
private:
int cardNumber;         // 1 - 52

public:
Card(int _cardNumber = 1) : cardNumber(_cardNumber) {}

void setCardNumber(int _cardNumber){
cardNumber = _cardNumber;
}

void setCard(int cardType, int suitType){    // cardType e {1..13}, suitType e {1..4}
cardNumber = (cardType-1) + (suitType-1)*13 + 1;
}

int getCardType(){   // returns 1 - 13
return (cardNumber-1) % 13 + 1;    // get card type 1-13 (ignore suit)
}

int getSuitType(){   // returns 1 - 4
return (cardNumber-1)/13 + 1;
}

int getValue(){   // returns 1-10
int valIgnoreSuit = getCardType();

if(valIgnoreSuit >= 10)
return 10;

return valIgnoreSuit;
}

int getAltValue(){   // returns 2-11 (ace can alternately be have a value of 11)
int val = getValue();

if(val == 1)
return 11;

return val;
}

string getCardName(){
static const char *cardName[] = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Unknown"};
int val = getCardType();

return cardName[val-1];
}

string getSuitName(){
static const char *suitName[] = {"Hearts", "Diamonds", "Spades", "Clubs"};
int val = getSuitType();

return suitName[val-1];
}
};

int main(){
Card deck[52];

for(int i = 1; i <= 52; i++){
deck[i].setCardNumber(i);
cout << deck[i].getCardName() << " of " << deck[i].getSuitName() << endl;
}
cin.get();
}``````
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.