Thread: Having problems with arrays!

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    16

    Having problems with arrays!

    Hey, I have made an array for a pack of cards:
    Code:
    char card[3][12] ={{"A",2,3,4,5,6,7,8,9,"T","J","Q","K"},
    		{"A",2,3,4,5,6,7,8,9,"T","J","Q","K"},
    		{"A",2,3,4,5,6,7,8,9,"T","J","Q","K"},
    		{"A",2,3,4,5,6,7,8,9,"T","J","Q","K"}};
    Unfortunately I get this error: error C2078: too many initializers.

    What am I doing wrong?

    Jon

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    you are putting 13 items per row (A-1-2....K) in a container designed to hold 12 items per row and 4 lines in a container to hold 3

    in addition you are trying to enter strings and integers into a container designed to hold characters.
    You're only born perfect.

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    16
    Ok I changed it to [4][13] but I'm still getting the same error. I only had it as [3][12] because I thought arrays started at 0.

    What do you mean by:
    in addition you are trying to enter strings and integers into a container designed to hold characters.
    and how do I fix that?

    Jon

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by jonnyfb
    Ok I changed it to [4][13] but I'm still getting the same error. I only had it as [3][12] because I thought arrays started at 0.
    They do, but when you declare them you declare them based on the size you need. If you need to hold 20 of something then you'd declare the array as size 20. When it comes to accessing those elements however (in a loop for example), then you need to start at 0 and go up to 19.

    Quote Originally Posted by jonnyfb
    What do you mean by:

    in addition you are trying to enter strings and integers into a container designed to hold characters.

    and how do I fix that?
    Code:
    char card[3][12];
    That is an array of characters... meaning it should only store characters. Items like "A", "T", "J", "Q", "K" are taken by the compiler to mean strings (in this context the address of the locations where those strings are stored... which cannot be converted to a character) because you are using double quotes " instead of single quotes '. You need to change those to 'A', 'T', 'J', 'Q', and 'K'. The integers will get converted to characters... but probably not the ones you are thinking of. You could make those '2', '3', '4', etc. as well, otherwise you'll need to think of a different way to represent your deck.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    16
    Hey thanks, I'll remember that!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problems with arrays
    By vikingcarioca in forum C Programming
    Replies: 4
    Last Post: 03-31-2009, 07:21 AM
  2. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  3. My Arrays!!!, My Arrays!!! Help!?
    By llcool_d2006 in forum C++ Programming
    Replies: 1
    Last Post: 12-10-2006, 12:07 PM
  4. Problems with Strings and Arrays.
    By SlyMaelstrom in forum C++ Programming
    Replies: 13
    Last Post: 04-15-2005, 02:13 PM
  5. Help!!! Problems with arrays.
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 05-08-2002, 08:21 PM