Hi, I'm currently working on a dice program in which I'm to create a function (called rollDice) that rolls a pair of dice until the sum of the numbers rolled is a specific number(given by user), and entered as the desired sum of the numbers to be rolled. The program must print the number of times the dice are rolled to get the desired sum and it must print the value of die1 and die2.
After looking at similar programs in my textbook, I came up with this code but it's still missing a lot things such as I still need to get my program to properly print out the number of times the dice were rolled, and to print out the value of die1 and die2.
I do know that I must include some parameters because the max sum of 2 dice is 12. So I've came up with this code but I don't know where to include it.
do
{
Cout << "enter the desired sum: " << endl;
cin>>num;
}
while (num < 2)|| num>12);
Any help is greatly appreciated, thanks.
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int num, rollDice(int num);
int main()
{
cout << “ Enter the number you want the two dice to equal: “;
cin >> num;
cout<< “The number of times the dice are rolled to get the sum” << num << “ = “ <<
rollDice(num) << endl;
return 0;
}
int rollDice(int num)
{
int die1;
int die2;
int sum;
int rollCount = 0;
srand(time(0));
do
{
die1 = rand() % 6 + 1;
die2 = rand() % 6 + 1;
sum = die1 + die2;
rollCount++;
}
while (sum != num);
return rollCount;
}
Yes thank you, and now to include my parameters do I replace
cout << “ Enter the number you want the two dice to equal: “;
cin >> num;
With my do statement to build parameters?
do
{
Cout << "enter the desired sum: " << endl;
cin>>num;
}