die roll simulator using functions

Hi there. I need help with my program. Basically it has to collect dice rolling statistics using functions. the output i'm getting from my code does not count anything. Please help. Thanks

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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
 #include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>

using namespace std;

int rollDie (int rand_number)
{	
	srand(time(0));
	 rand_number = (rand() % 6) + 1 ;
		 return rand_number;

}


int main()
	
	{
		int userChoice, i;
		int diceTotal;
		int two, three, four, five, six, seven, eight, nine, ten, eleven, twelve;
		two = 0;
		three = 0;
		four = 0;
		five = 0;
		six = 0;
		seven = 0;
		eight = 0;
		nine = 0;
		ten = 0;
		eleven = 0;
		twelve = 0;
		i = 0;

		cout<< "How many times would you like to roll the die?" ;
		cout<< endl;
		cin>> userChoice;

		diceTotal = rollDie(6) + rollDie(6);
		
		while (i <= userChoice) {
		if (diceTotal == '2') 
			two++;
		else if (diceTotal == '3')
			three++;
		else if (diceTotal == '4')
			four++;
		else if (diceTotal == '5')
			five++;
		else if (diceTotal == '6')
			six++;
		else if (diceTotal == '7')
			seven++;
		else if (diceTotal == '8')
			eight++;
		else if (diceTotal == '9')
			nine++;
		else if (diceTotal == '10')
			ten++;
		else if (diceTotal == '11')
			eleven++;
		else if (diceTotal == '12')
			twelve++;
		i++;
		}
		

		cout<< "Total" << "    Number of totals rolled" << "     " << "Probability" << endl;
		cout<< "2   " << two << "     " <<endl;
		cout<< "3   " << three << "     " <<endl;
		cout<< "4   " << four << "     " <<endl;
		cout<< "5   " << five << "     " <<endl;
		cout<< "6   " << six << "     " <<endl;
		cout<< "7   " << seven << "     " <<endl;
		cout<< "8   " << eight << "     " <<endl;
		cout<< "9   " << nine << "     " <<endl;
		cout<< "10   " << ten << "     " <<endl;
		cout<<"11   " <<  eleven << "     " <<endl;
		cout <<"12   " << twelve << "        "<<endl; 
		
}


You only need to seed the random function once at the beginning of the program, so you can move the srand line out of the function and to the beginning of main.

diceTotal is an integer, so you want to check for 1,2,3.. etc. instead of '1','2','3' etc.

The rolling of the dice is outside the while loop, so right now you're only doing one roll of the dice.

i is initialized to 0, so if a user enters 5, you actually end up looping through the while loop 6 times.

I might suggest using an array or vector instead of having separate variables for each roll type.

Edit:
You could also use a for loop since you know how many times you need to roll the dice. Let's say you had an array rollCounter with 11 elements.
for (as many times as the user wants to roll the dice)
--- diceTotal = result of roll function
--- rollCounter[diceTotal-1] increments by 1
Last edited on
else if (diceTotal == '2') Thats a char, not an int. Your rolldie method returns an int

Try this:
else if (diceTotal == 2)
hi wildblue & Jakee, Thank you so much for the help. I followed your comments mentioned above and was actually able to fix my code and got my program working. I can't use array because my class prof has not taught us anything about it yet. But regardless, I got my program working. Thanks alot! :)
Topic archived. No new replies allowed.