0
#include<stdio.h>

char count[3][5][14]={{"♠1","♠2","♠3","♠4","♠5","♠6","♠7","♠8","♠9","♠10","♠J","♠Q","♠K"},
                            {"◇1","◇2","◇3","◇4","◇5","◇6","◇7","◇8","◇9","◇10","◇J","◇Q","◇K"},
                            {"♣1","♣2","♣3","♣4","♣5","♣6","♣7","♣8","♣9","♣10","♣J","♣Q","♣K"},
                            {"♡1","♡2","♡3","♡4","♡5","♡6","♡7","♡8","♡9","♡10","♡J","♡Q","♡K"};

I want to declare this type of array but it always makes errors such as"Too many initializers". How can I fix this error?

9
  • 3
    You declare a 3 dimensions array. For first dimension, you have written 3, but give 4 elements, so yes, too many initializers. And you use c++, so use containers (like std::vector) instead of raw array Commented Oct 18, 2017 at 13:34
  • I think perhaps you expect index [3] to correspond to the 3 characters in your "♠1"? If so the array declaration order is wrong, should be char count[5][14][3] Commented Oct 18, 2017 at 13:37
  • 3
    That is a [4][13][3] array. Commented Oct 18, 2017 at 13:39
  • 1
    I think you want a std::string count[4][13] 2D array instead. Commented Oct 18, 2017 at 13:41
  • 1
    That's a 3d char array or 2d string array, that you are asking for, not a 3d string array. Commented Oct 18, 2017 at 13:44

2 Answers 2

3

What you want to do is maybe this:

const char * count[4][13]= {{"♠1","♠2","♠3","♠4","♠5","♠6","♠7","♠8","♠9","♠10","♠J","♠Q","♠K"},
                            {"◇1","◇2","◇3","◇4","◇5","◇6","◇7","◇8","◇9","◇10","◇J","◇Q","◇K"},
                            {"♣1","♣2","♣3","♣4","♣5","♣6","♣7","♣8","♣9","♣10","♣J","♣Q","♣K"},
                            {"♡1","♡2","♡3","♡4","♡5","♡6","♡7","♡8","♡9","♡10","♡J","♡Q","♡K"}};

Anyway as already suggested std::vector and std::string should be preferred

Sign up to request clarification or add additional context in comments.

2 Comments

You store a pointer to constant string, please, use at least const char *, or const char * const
@Garf365 approved :)
2

The C++ way would be using a string class like std::string and a container like std::vector (not raw C-style char strings and raw arrays), e.g.:

 vector<vector<vector<string>>> x;

If what you really want is a two-dimension string array, then that would be:

 vector<vector<string>> x;

2 Comments

Excellent answer, probably not one expected (see molbdnilo comment) but, actually the best IMHO
Seems more likely that the OP is asking to learn rather than to do the best application, so IMHO it is better to give him the array solution 1st, and suggest the vector solution 2nd.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.