CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

Thread: Constant array question? (Begginers question)

1. Member
Join Date
May 2006
Posts
102

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!

2. Senior Member
Join Date
Nov 2006
Location
Australia
Posts
1,569

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.

3. Member
Join Date
Nov 2007
Posts
46

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```
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.

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..

4. Senior Member
Join Date
Nov 2006
Location
Australia
Posts
1,569

Re: Constant array question? (Begginers question)

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).

5. Member
Join Date
Nov 2007
Posts
46

Re: Constant array question? (Begginers question)

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. : * (

6. Senior Member
Join Date
Nov 2006
Location
Australia
Posts
1,569

Re: Constant array question? (Begginers question)

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.

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

Click Here to Expand Forum to Full Width