Let a user choose how many sides are on a die and how many times it is rolled. Output the sum of all the numbers rolled.

Having some trouble on the last part of this question. I was able to make a die that rolls randomly and outputs the number. I was able to make 2 die roll randomly and output the sum of both. I was also able to let the user decide how many sides the die would have and output the random side it lands on. Now, I need to let the user choose the number of sides AND the number of times it is rolled. I then need to get the sum of all the numbers that were rolled. Here is what I have so far

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <iostream>
#include <cstdlib>
#include <time.h>
using namespace std;


int oneDie() {
	int rollOneDie;
	int min = 1;
	int max = 6;

	rollOneDie = rand() % (max - min + 1) + min;

	return rollOneDie;
}

int twoDie() {
	int rollOne;
	int rollSecond;
	int min = 1;
	int max = 6;

	rollOne = rand() % (max - min + 1) + min;
	rollSecond = rand() % (max - min + 1) + min;

	int rollTotal = rollOne + rollSecond;
	
	return rollTotal;

}

int userSides() {
	int userRoll;
	int min = 1;
	int max;

	cout << "Enter how many sides you want on the die: ";
		cin >> max;

		userRoll = rand() % (max - min + 1) + min;
		return userRoll;

}

int userRolls() {
	int userRoll;
	int diceNum;
	int min = 1;
	int max;

	cout << "Enter how many sides you want on the die: ";
	cin >> max;

	userRoll = rand() % (max - min + 1) + min;
	
	cout << "Enter how many dice you want to roll: ";
	cin >> diceNum;
	
	return diceNum;
	return userRoll;


}
int main() {

	
		cout << oneDie() << endl;
		cout << twoDie() << endl;
		cout << userDie() << endl;

}
Last edited on
http://www.cplusplus.com/doc/tutorial/control/ (see iteration)
1
2
3
4
5
def nDie(sides, times):
   sum = 0
   repeat times:
      sum = sum + roll(sides)
   return sum
Topic archived. No new replies allowed.