Question on arrays

This is a discussion on Question on arrays within the C Programming forums, part of the General Programming Boards category; For school I have to program a Blackjack game. I'm using CodeBlocks. Now it gives an error on a certain ...

  1. #1
    T1m
    T1m is offline
    Registered User
    Join Date
    Dec 2008
    Posts
    6

    Question on arrays

    For school I have to program a Blackjack game.

    I'm using CodeBlocks.

    Now it gives an error on a certain part of my code:

    Code:
    int harten[13][2];
    int ruiten[13][2];
    int schoppen[13][2];
    int klaveren[13][2];
    
    InitialiseCards(){
    	harten[13][2] = {{1,1},{2,1},{3,1},{4,1},{5,1},{6,1},{7,1},{8,1},{9,1},{10,1},{11,1},{12,1},{13,1}};
    
    	ruiten[13][2] = {{1,1},{2,1},{3,1},{4,1},{5,1},{6,1},{7,1},{8,1},{9,1},{10,1},{11,1},{12,1},{13,1}};
    
    	schoppen[13][2]= {{1,1},{2,1},{3,1},{4,1},{5,1},{6,1},{7,1},{8,1},{9,1},{10,1},{11,1},{12,1},{13,1}};
    
    	klaveren[13][2]= {{1,1},{2,1},{3,1},{4,1},{5,1},{6,1},{7,1},{8,1},{9,1},{10,1},{11,1},{12,1},{13,1}};
    }
    (harten, ruiten, schoppen and klaveren are the dutch names of the type of cards)

    it could be a very stupid fault because I'm just a newbie, but I realy can't figure it out

    I hope someone can help me
    Last edited by T1m; 12-30-2008 at 10:02 AM.

  2. #2
    Why am I a programmer? shoutatchickens's Avatar
    Join Date
    Mar 2008
    Posts
    45
    Can you give the specific error?

    Also, can you explain why you are using 2d arrays?
    One of the disadvantages of being a 22 year old RPG programmer is having to repeatedly explain to your friends that you don't make videogames for a living.

  3. #3
    T1m
    T1m is offline
    Registered User
    Join Date
    Dec 2008
    Posts
    6
    the error:

    Code:
    error: syntax error before '{' token
    the error points to this rule:

    Code:
    harten[13][2] = {{1,1},{2,1},{3,1},{4,1},{5,1},{6,1},{7,1},{8,1},{9,1},{10,1},{11,1},{12,1},{13,1}};
    I'm using 2D arrays, because I want to make shure that a card isn't used 2 times

    sow in the first part I keep the value of the card (1 - 13) and in the second part if the card has been taken allready (1 or 0)

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    62
    You cannot assign an array like this. Exception: When the array is defined.

    Code:
    int i[5] = {1, 2, 3, 4, 5}; /* ok */
    Code:
    int i[5]; /* definition is here */
    ...
    i =  {1, 2, 3, 4, 5}; /* fails, too late to initialize */
    So move the initialization to the point where the arrays are defined or use a loop to fill the array:

    Code:
    for (i = 0; i < 14; ++i) {
        harten[i][0] = i + 1;
        harten[i][1] = 1;
    }

  5. #5
    Why am I a programmer? shoutatchickens's Avatar
    Join Date
    Mar 2008
    Posts
    45
    I believe that initializing the array as you are trying to do can only be done at the declaration of the variable.

    So inside of your initialization function, you coudl use a loop to assign the values to the array.

    edit:
    (i.e. see post above mine )
    One of the disadvantages of being a 22 year old RPG programmer is having to repeatedly explain to your friends that you don't make videogames for a living.

  6. #6
    T1m
    T1m is offline
    Registered User
    Join Date
    Dec 2008
    Posts
    6
    ok, thank you very much guys!

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,188
    Also, as you should know, using the [] references a single element in the array, not the entire array. So how could you possibly assign several values to a single element?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about arrays.
    By Kelvie in forum C++ Programming
    Replies: 3
    Last Post: 09-17-2007, 05:32 AM
  2. A question concerning character arrays
    By ellipses in forum C Programming
    Replies: 3
    Last Post: 03-08-2005, 07:24 PM
  3. Replies: 6
    Last Post: 04-26-2004, 10:02 PM
  4. Question about char arrays
    By PJYelton in forum C++ Programming
    Replies: 5
    Last Post: 10-21-2003, 12:44 AM
  5. Question about arrays?? PLease read..
    By foofoo in forum C Programming
    Replies: 3
    Last Post: 06-24-2002, 02:40 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21