# Constant array question? (Begginers question)

• April 13th, 2009, 09:15 PM
Ulnarian
Constant array question? (Begginers question)
Hey all, just for fun I'm designing a game that is similar to Yahtzee. In order to store the values of the players rolls, I'm envisioning an integer array something like:

int roll_value[10];

Where roll_value[0] could be the value of a "two of a kind" roll and roll_value[1] could be the value of a "three of a kind" roll.

I've hit a conceptual roadblock. Once the value of the roll_value array has been declared, how can I ensure that the player cannot put any further values into that element? Would const do the trick somehow?

Thanks!
• April 13th, 2009, 09:22 PM
Mybowlcut
Re: Constant array question? (Begginers question)
If the values of roll_value are constant then you'd want to use an enum instead.

If the values aren't constant then you'd just limit how many elements you access when you are looping through it. Not quire sure I understand your question though.
• April 13th, 2009, 09:27 PM
TheDominis
Re: Constant array question? (Begginers question)
Why use an array when some defines would do the trick? This would also save you the hassle of having to remember that roll_value[0] is two of a kind, roll_value[1] is three of a kind, etc.

Code:

```#define TWO_OF_A_KIND 0 #define THREE_OF_A_KIND 1 //etc```
Quote:

I've hit a conceptual roadblock. Once the value of the roll_value array has been declared, how can I ensure that the player cannot put any further values into that element?
Don't write code that allows the user to feed the computer data to set the values in that array.

Quote:

Would const do the trick somehow?
Code:

`const int roll_value[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };`
The const keyword will just allow for compile-time checking for assignments..
• April 13th, 2009, 09:41 PM
Mybowlcut
Re: Constant array question? (Begginers question)
Quote:

Originally Posted by TheDominis
Why use an array when some defines would do the trick?
Code:

```#define TWO_OF_A_KIND 0 #define THREE_OF_A_KIND 1 //etc```

See Meyer's Effective C++ item 2: Prefer consts, enums, and inlines to #defines as to why that's not the best solution (especially for a beginner who needs to learn the best practices/habits).
• April 13th, 2009, 10:21 PM
TheDominis
Re: Constant array question? (Begginers question)
Quote:

Originally Posted by Mybowlcut
See Meyer's Effective C++ item 2: Prefer consts, enums, and inlines to #defines as to why that's not the best solution (especially for a beginner who needs to learn the best practices/habits).

I was just offering a solution to his problem. : * (
• April 14th, 2009, 01:17 AM
Mybowlcut
Re: Constant array question? (Begginers question)
Quote:

Originally Posted by TheDominis
I was just offering a solution to his problem. : * (

Haha don't take it personally. I was just letting you know that in the interest of bettering the OP's programming practices, there is a better way to do what you were suggesting.