Dice Program

My objective in this program was to write a function that rolls a pair of dice until the sum of the numbers rolled is a specific number(given by the user). It is also suppose to display the number of times the dice are rolled to get the desired sum. If everything looks ok, How do I allow the user to call the rollDice function as many times as they want?

#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;
}

You could place the code in a while loop (the code that asks the user for input)

I.e.

1
2
3
4
5
6
7
8
bool running=true;
while(running)
{
//code here
//roll again?
//get input
//if no, running = false
}


and provide a prompt to roll again, depending on user input set bool to false or continue.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#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;
}
do code tags please. second you are doing this
1
2
int num, rolldice(int num);
int main()
and this
1
2
3
4
5
}
Int rollDice(int num)
{
int die1;
int die2;
do you notice an error here?
Topic archived. No new replies allowed.