Thread: Question using some arrays...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    15

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

  2. #2
    Registered User
    Join Date
    Jan 2003
    Posts
    35
    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. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    15
    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!
    Im special

  4. #4
    Funniest man in this seat minesweeper's Avatar
    Join Date
    Mar 2002
    Posts
    798
    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;
    Last edited by minesweeper; 01-14-2003 at 09:03 PM.

  5. #5
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    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. #6
    Funniest man in this seat minesweeper's Avatar
    Join Date
    Mar 2002
    Posts
    798
    Yeah I've noticed that before. Maybe MS are trying to set a new standard

  7. #7
    Registered User
    Join Date
    Sep 2002
    Posts
    15
    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?
    Im special

  8. #8
    Funniest man in this seat minesweeper's Avatar
    Join Date
    Mar 2002
    Posts
    798
    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. #9
    Registered User
    Join Date
    Jan 2003
    Posts
    35
    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.

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, 08: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