Hi, I am new at c++ and am currently trying to write a function that throws 10 six-faced dices. However, I need to use the results of the dice throws in another function (that puts them in an array). How can I achieve that. For now this is the only thing I have:

int diceThrow()
    int a; 
    int lowest=1, highest=6; 
    int range=(highest-lowest)+1; 
    for(int index=0; index<10; index++){ 
        a = lowest+int(range*rand()/(RAND_MAX + 1.0)); 
        cout << a << endl;

As you can see, this function can only tell me what the results are and can't return them to another function.

8 Years
Discussion Span
Last Post by VernonDozier

You can return a pointer, but you cannot return an array. You can also pass the array to the function and have the function fill it in:

void diceThrow (int array[], int numRolls)
   // fill in array with random dice rolls

int main ()
   int diceRolls[10];
   diceThrow (diceRolls, 10);
   // diceRolls array is now filled in and usable in main or any function that the array is passed to.
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.