# Thread: Question using some arrays...

1. ## Question using some arrays...

Ok what im trying to do is have roll 5 dice, keep the ones that are equal off to the side, and roll the remaining ones over again.(like in yahtzee) The info that i have gathered so far is this:

int dice[5];

int again[5]={ 1,1,1,1,1 };

for(int i=0;i<5;i++){
if(again[i]==1)dice[i]=1+rand()%6;

and to roll them over i was told to use: again[i]=0;

I havent got into arrays much yet, so im not sure how to put this stuff together. Is there anyone that could quickly write a small program for me, and post it or email it? If what im trying to do will turn out to be some huge thing, say so and i'll have to think up another game. any tips or ideas will help. thanks a bunch.

2. It's not a huge thing.

I'm risking writing your program for you but I like a project so here's my stab.

Throw it into your compiler & it will work. I'm sure there's a more concise way of doing it but I just wrote this out and it does the trick if I know what you want and so there you go.

Code:
```#include <stdio.h>
#include <iostream.h>
#include <stdlib.h>
#include <time.h>

int numDice=5;
int diceArray[5]={0,0,0,0,0};
bool same[5] = {0,0,0,0,0};
bool choice;

int RandomNumber(int iMin, int iMax)
{
return((rand() % (abs(iMax-iMin)+1))+iMin);
}

int RollDice()
{
return(RandomNumber(1,6));
}

int CheckForMatches()
{
int matches=0;
for(int i=0; i<numDice; i++)
if(same[i]) matches++;
return matches;
}

int main(int argc, char *argv[])
{
srand( (unsigned)time( NULL ) );

do
{
for(int i=0; i<numDice; i++)
if(!same[i]) diceArray[i]=RollDice();
for(int i=0; i<numDice; i++)
for(int j=0; j<numDice; j++)
if(diceArray[i] == diceArray[j] && i != j)
same[i]=true;
for(int i=0; i<numDice; i++)
{
if(same[i])
cout << "    ";
cout << diceArray[i] << "\n";
}
cout << "\n\n\n";
if(CheckForMatches()<5)
{
if(!CheckForMatches())
cout << "There were no matches.  Roll dice again?\n";
else
cout << "You got some matches!  Roll remaining dice?\n";
cout << "1 - Yes\n0 - No\n\n";
}
else
cout << "They're all matched up!  Enter 0 to quit.\n";
cin >> choice;
cout << "\n\n\n\n";
} while(choice);

return 0;
}```
If you want me to explain anything just ask.

BTW arrays are tricky, I'm having some of my own problems with them (see thread).

3. hey thats awesome, although when i put it in i get 3 errors. it might just be my comp... but here they are:

'i' : redefinition; multiple initialization
see declaration of 'i'

'i' : redefinition; multiple initialization
see declaration of 'i'

binary '>>' : no operator defined which takes a right-hand operand of type 'bool' (or there is no acceptable conversion)

for now im going to sit down and look at it and learn from it. thanks a bunch!

4. you are declaring 'i' lots of times here

Code:
```for(int i=0; i<numDice; i++)
if(!same[i]) diceArray[i]=RollDice();
for(int i=0; i<numDice; i++)
for(int j=0; j<numDice; j++)
if(diceArray[i] == diceArray[j] && i != j)
same[i]=true;
for(int i=0; i<numDice; i++)```
And the other error must mean you can't use cin>> with a bool as in
Code:
`cin >> choice;`

5. As far as I know, it's a bug in MSVC++. The standard states that a variable declared like that will only exist for the duration of the for-loop.

In the case of MSVC, it does not.

6. Yeah I've noticed that before. Maybe MS are trying to set a new standard

7. does this mean i wont be able to run the program? or can i change one of those 'i's to something else and have it work?

8. just remove 'int' from all but the first 'for' loop or use different variable names e.g. int i, int j, int k etc.

9. That' why I use Dev-C++ instead of MSVC(plus Dev from Bloodshed is 100% free).

Are you serious though that MSVC still doesn't let the for index go out of scope?

Should be able to cin bools. This program works perfectly in Dev.