Please help with dice roll program

Hello,

I'm new to c++ and I have this problem.
1) Write a program to simulate rolling a pair of dice 100 times.
2) Write the logic so that:
** When the result is less than 6, display "....."
** When the result is 6, 7, or 8, display "......"
** When the result is greater than 8, display "....."
3) Record how many times each value (2, 3, 4, ...12) occurs and how many rolls in total.
4) Display the numbers of times each outcome occurred and its percent of the total numbers of rolls.

I am fine until I get to number 3... I'm drawing a blank on how to count how many times each number occurs. I have the total rolls figured out. And then I'm stuck on number 4 on how to count each outcome to figure out the percent.

** The bottom of my code is not complete I just typed out the cout where I will put the results from numbers 3 and 4

** And we haven't learned functions yet so I cant use anything like that.

Any help would be greatly appreciated!!

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
#include <iostream>
#include <cstdlib>
#include <iomanip>
using namespace std;
int main()

{
	int die1, die2, total_dice, count, i;
	
	srand(time(0));

	count = 0;

	for (i = 1; i <= 100; i++)
	{
		die1 = rand() % 6 + 1;
		die2 = rand() % 6 + 1;
		total_dice = die1 + die2;
		cout << "Result from roll is: " << total_dice << endl;
		count ++;
		
		if (total_dice < 6)
		{
			cout << "Hypertron battery failure imminent, MAYDAY, MAYDAY!!" << endl;
			cout << "" << endl;
		}

		if  (total_dice == 6 || total_dice == 7 || total_dice == 8)
		{
			cout << "Normal function, continue operations" << endl;
			cout << "" << endl;
		}

		if (total_dice > 8 )
		{
			cout << "I've never seen such performance, Dr. Spock.  It's a miracle!!" << endl;
			cout << "" << endl;
		}
	}

	cout << "Total Rolls: " << count << endl;
	cout << "" << endl;
	
	cout << "" << endl;
	cout << "The number " << " " << " occured " << "" << " times and its 
        percentage of occurence is " << "" << endl;
	cout << "The number " << " " << " occured " << "" << " times and its    
        percentage of occurence is " << "" << endl;
	cout << "The number " << " " << " occured " << "" << " times and its 
        percentage of occurence is " << "" << endl;
	cout << "The number " << " " << " occured " << "" << " times and its 
        percentage of occurence is " << "" << endl;
	cout << "The number " << " " << " occured " << "" << " times and its 
        percentage of occurence is " << "" << endl;
	cout << "The number " << " " << " occured " << "" << " times and its 
         percentage of occurence is " << "" << endl;
	cout << "The number " << " " << " occured " << "" << " times and its 
        percentage of occurence is " << "" << endl;
	cout << "The number " << " " << " occured " << "" << " times and its 
        percentage of occurence is " << "" << endl;
	cout << "The number " << " " << " occured " << "" << " times and its 
        percentage of occurence is " << "" << endl;
	cout << "The number " << " " << " occured " << "" << " times and its 
        percentage of occurence is " << "" << endl;
	cout << "The number " << " " << " occured " << "" << " times and its 
        percentage of occurence is " << "" << endl;
	
	return 0;
}
Have you learned arrays?

You have to count up how many times each of the twelve possible total_dice values occurs. So if you have an array of twelve integers and increment the appropriate one every time that total_dice value occurs, you'll end up with the total of each at the end.

Jim
Jim,

Unfortunately we haven't learned arrays yet. Our next topic is functions then arrays. Is there another possible way to do it?

Emilio
If you're not allowed to use arrays, I'm guessing your teacher's intent is to have you write this without to show how 'ugly' the code is, and then show you how 'beautiful' it is with an array.

You're going to have to have twelve separate variables, one for each possible total_dice result, and increment the appropriate one in a big switch statement. You could use a big set of if-else statements, but that's ungainly. If you've learned switch statements use that ...

Jim
closed account (Dy7SLyTq)
have u learned pointers?
@Jim, Yes I do believe you are correct where the teacher wants us to write "ugly" code then shows us how to do it properly since our next topic is functions & arrays. And yes we have learned switch statement, but could you give me an example of how you mean to set it up? I just cant seem to picture it for some reason.

@DTSCode, and no we have not yet learned pointers.
1
2
3
4
5
6
7
8
9
10
11
12
13
    unsigned roll_1 = 0 ;
    unsigned roll_2 = 0 ;
    // ...
    unsigned roll_12 = 0 ;

    // ...
    switch( current_roll )
    {
     case 1: ++roll_1 ; break ;
     case 2: ++roll_2 ; break ;
     // ....
     case 12: ++roll_12 ; break ;
    }
@cire - thank you. Ill give it a shot later after classes.
Ok so I got the switch to work but for some reason cant get the percentage to work properly. I keep getting an output of 0. I have done percents before so what am I missing here?

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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <iostream>
#include <cstdlib>
#include <iomanip>
using namespace std;
int main()

{
	int die1, die2, total_dice, count, i, roll_2, roll_3, roll_4, roll_5, roll_6, roll_7, roll_8, roll_9, roll_10, roll_11, roll_12 ;
	
	srand(time(0));

	count = 0;
	roll_2 = 0;
	roll_3 = 0;
	roll_4 = 0;
	roll_5 = 0;
	roll_6 = 0;
	roll_7 = 0;
	roll_8 = 0;
	roll_9 = 0;
	roll_10 = 0;
	roll_11 = 0;
	roll_12 = 0;

	for (i = 1; i <= 100; i++)
	{
		die1 = rand() % 6 + 1;
		die2 = rand() % 6 + 1;
		total_dice = die1 + die2;
		cout << "Result from roll is: " << total_dice << endl;
		count ++;
		
		if (total_dice < 6)
		{
			cout << "Hypertron battery failure imminent, MAYDAY, MAYDAY!!" << endl;
			cout << "" << endl;
		}

		if  (total_dice == 6 || total_dice == 7 || total_dice == 8)
		{
			cout << "Normal function, continue operations" << endl;
			cout << "" << endl;
		}

		if (total_dice > 8 )
		{
			cout << "I've never seen such performance, Dr. Spock.  It's a miracle!!" << endl;
			cout << "" << endl;
		}

		switch (total_dice)
		{
			case 2: ++roll_2 ; break ;
			case 3: ++roll_3 ; break ;
			case 4: ++roll_4 ; break ;
			case 5: ++roll_5 ; break ;
			case 6: ++roll_6 ; break ;
			case 7: ++roll_7 ; break ;
			case 8: ++roll_8 ; break ;
			case 9: ++roll_9 ; break ;
			case 10: ++roll_10 ; break ;
			case 11: ++roll_11 ; break ;
			case 12: ++roll_12 ; break ;
		}

	}

	int percent_2;
	int percent_3;
	int percent_4;
	int percent_5;
	int percent_6;
	int percent_7;
	int percent_8;
	int percent_9;
	int percent_10;
	int percent_11;
	int percent_12;

	percent_2 = ((roll_2/100) * 100);
	percent_3 = ((roll_3/100) * 100);
	percent_4 = ((roll_4/100) * 100);
	percent_5 = ((roll_5/100) * 100);
	percent_6 = ((roll_6/100) * 100);
	percent_7 = ((roll_7/100) * 100);
	percent_8 = ((roll_8/100) * 100);
	percent_9 = ((roll_9/100) * 100);
	percent_10 = ((roll_10/100) * 100);
	percent_11 = ((roll_11/100) * 100);
	percent_12 = ((roll_12/100) * 100);

	cout << "Total Rolls: " << count << endl;
	cout << "" << endl;
	

	cout << "The number 2 occured " << roll_2 << " times and its percentage of occurence is " << percent_2 << " %" << endl;
	cout << "The number 3 occured " << roll_3 << " times and its percentage of occurence is " << percent_3 << " %" << endl;
	cout << "The number 4 occured " << roll_4 << " times and its percentage of occurence is " << percent_4 << " %" << endl;
	cout << "The number 5 occured " << roll_5 << " times and its percentage of occurence is " << percent_5 << " %" << endl;
	cout << "The number 6 occured " << roll_6 << " times and its percentage of occurence is " << percent_6 << " %" << endl;
	cout << "The number 7 occured " << roll_7 << " times and its percentage of occurence is " << percent_7 << " %" << endl;
	cout << "The number 8 occured " << roll_8 << " times and its percentage of occurence is " << percent_8 << " %" << endl;
	cout << "The number 9 occured " << roll_9 << " times and its percentage of occurence is " << percent_9 << " %" << endl;
	cout << "The number 10 occured " << roll_10 << " times and its percentage of occurence is " << percent_10 << " %" << endl;
	cout << "The number 11 occured " << roll_11 << " times and its percentage of occurence is " << percent_11 << " %" << endl;
	cout << "The number 12 occured " << roll_12 << " times and its percentage of occurence is " << percent_12 << " %" << endl;
	
	return 0;
}
Last edited on
percent_2 = ((roll_2/100.0) * 100);

Without a decimal in there, it's all integer math.
Yes that's right, now I remember that. I made the same mistake last time :-)

Thanks again for your help cire!!
Topic archived. No new replies allowed.