user2105982
user2105982

Reputation: 187

Initializing a card deck using unicode

I'm trying to create a card deck array that holds 52 card structs that contain an integer and a "pic" which will be the unicode representation of suit. I've tried creating a constant character for each suit with the unicode values, but this isn't quite working. Is there a unicode library I need to get? here is my code:

#ifndef CARD_H
#define CARD_H
#include <iostream>
using namespace std;

class Card 
{

private:

struct OneCard
{
    int value;
    char Pic[4];
};

OneCard Cards[52];



public:
Card();

 };

#endif



#include "Card.h"
#include <iostream>
using namespace std;

Card::Card()
{
const char spade[4]="\xe2\x99\xa0";
const char club[4]="\xe2\x99\xa3";
const char heart[4]="\xe2\x99\xa5";
const char diamond[4]="\xe2\x99\xa6";

for (int i = 0; i<13; i++)
{
    Cards[i].value=i+1;
    Cards[i].Pic=spade;
}

for (int i = 13; i<26; i++)
{
    Cards[i].value=i+1;
    Cards[i].Pic=club;
}
for (int i = 26; i<39; i++)
{
    Cards[i].value=i+1;
    Cards[i].Pic=heart;
}
for (int i = 39; i<52; i++)
{
    Cards[i].value=i+1;
    Cards[i].Pic=diamond;
}


}

Upvotes: 0

Views: 200

Answers (1)

SwiftMango
SwiftMango

Reputation: 15294

The upper bound of your loops are wrong. And you cannot just assign an array to another array.

Add this #include <algorithm> and change to the code below:

for (int i = 0; i<13; i++)
{
    Cards[i].value=i+1;
    std::copy(spade, spade+3, Cards[i].Pic);
}

for (int i = 13; i<26; i++)
{
    Cards[i].value=i+1;
    std::copy(club, club+3, Cards[i].Pic);
}
for (int i = 26; i<39; i++)
{
    Cards[i].value=i+1;
    std::copy(heart, heart+3, Cards[i].Pic);
}
for (int i = 39; i<52; i++)
{
    Cards[i].value=i+1;
    std::copy(diamond, diamond+3, Cards[i].Pic);
}

Upvotes: 1

Related Questions