The following code takes a user-provided number of sides for the dice, and then calls a function to return the random result of the dice roll. However, I can't figure out how to generate a random number between 1 and the number provided.
#include <iostream>
using namespace std;
int main() {
int numberOfSides = 0;
do
{
cout << "How many sides do you want the die to have? ";
cin >> numberOfSides;
if (numberOfSides <= 0)
{
cout << "Error: Please enter a positive integer!" << endl << endl;
}
} while (numberOfSides <= 0);
int result = rollDie(numberOfSides);
cout << "You rolled a " << result << endl;
}
int rollDie(int numberOfSides)
{
// Complete this function
}