8

I am working on a BlackJack Game using C++, and I have the following piece of code within it where I am getting an error

typedef struct
{
    int value;
    char suit[8];
    char name[8];
}Deck;


Deck Cards[52] = {{ 1,"Ace","Hearts"},{ 2, "Two","Hearts"}, { 3, "Three", "Hearts"}, { 4, "Four","Hearts"}, { 5,"Five","Hearts"},{ 6,"Six", "Hearts06"},
{ 7,"Seven","Hearts"},{ 8,"Eight","Hearts"},{ 9,"Nine","Hearts"},{ 10,"Ten","Hearts"},{10,"Jack","Hearts"},{10,"Queen","Hearts"},{10,"King","Hearts"},
{ 1,"Ace","Clubs"},{2, "Two", "Clubs"},{3,"Three","Clubs"},{4,"Four","Clubs"},{5,"Five","Clubs"},{6,"Six","Clubs"},{7,"Seven","Clubs"},{8,"Eight","Clubs"},
{ 9,"Nine","Clubs"},{10,"Ten","Clubs"},{10,"Jack","Clubs"},{10,"Queen","Clubs"},{10,"King","Clubs"},
{ 1,"Ace","Diamonds"},{2,"Two","Diamonds"},{3,"Three","Diamonds"},{4,"Four","Diamonds"},{5,"Five","Diamonds"},{6,"Six","Diamonds"},{7,"Seven","Diamonds"},
{ 8,"Eight","Diamonds"},{9,"Nine","Diamonds"},{10,"Ten","Diamonds"},{10,"Jack","Diamonds"},{10,"Queen","Diamonds"},{10,"King","Diamonds"},
{ 1,"Ace","Spades"},{ 2,"Two","Spades"},{3,"Three","Spades"},{4,"Four","Spades"},{5,"Five","Spades"},{6,"Six","Spades"},{7,"Seven","Spades"},
{ 8,"Eight","Spades"},{ 9,"Nine","Spades"},{10,"Ten","Spades"},{10,"Jack","Spades"},{10,"Queen","Spades"},{10,"King","Spades"}};

The error is

Main.c:39:127: error: initializer-string for array of chars is too long [-fpermissive]

Line 39 is the last line in the code posted above

Please help me in figuring out why the compiler is throwing an error

Andon M. Coleman
  • 42,359
  • 2
  • 81
  • 106
Praveen Gowda I V
  • 9,569
  • 4
  • 41
  • 49
  • Shouldn't the size of the array be 52? Also I assume "Hearts06" was not what you wanted. – drescherjm Jan 28 '14 at 14:17
  • You could use `std::string`s and `std::vector`s. Then you couldn't make mistakes with array sizes. – eerorika Jan 28 '14 at 14:18
  • @drescherjm it is 52 in the code, I was experimenting by changing it, I have edited the question – Praveen Gowda I V Jan 28 '14 at 14:19
  • 2
    Have fun programming the logic whereby an Ace can be treated as 1 or 11 – CashCow Jan 28 '14 at 14:36
  • 3
    @CashCow If you can't help, then please don't. I was asking for help to figure out what was causing the error, and not to do code review for best practices for implementing BlackJack in C++ – Praveen Gowda I V Jan 29 '14 at 12:45
  • The question had been answered, although you might notice that you have put "suits" and "names" the wrong way round. The compiler won't help you on that, it doesn't know what is a suit and what is a name. – CashCow Jan 29 '14 at 15:27

2 Answers2

20

The string "Diamonds" has 9 characters including the null terminating character. Therefore, name must have at least 9 elements.

However, it looks like your name member should be called suit and vice versa.

Joseph Mansfield
  • 108,238
  • 20
  • 242
  • 324
3

Instead of:

int nMyArray[8]= {5,6,5,4,6,7,4,2};

Prefer this:

int nMyArray[]= {5,6,5,4,6,7,4,2};

When you are initializing an array. The former one requires you to specify size. The latter one computes the size (at compile time only).

Ajay
  • 18,086
  • 12
  • 59
  • 105