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()
{
  srand((unsigned)time(0));
    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.

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.
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.