Rolling a Dice and counting Results

Hey everybody,

I just a wrote a procedural C++ Program which "rolls a dice" and counts the amount
of 1,2,3,4,5 or 6.

For some reason, it doesn't work...
I've been on this for quite some time, but I don't see my fault.

Would be awesome if you guys helped me :)


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 <cstdio>
#include <cstdlib>

using namespace std;
	
int main(int argc, char* argv[])
{

int total1=0, total2=0, total3=0, total4=0, total5=0, total6=0 ;
int dicenumber;

cout<<"Dice-Results: \n";
	
for(int i=0;i<20;i++)
{
		
	dicenumber = 1+(rand()%6);
			
switch(dicenumber)
	{	
case '1':
	
	total1=total1+1;
	break;		
	
case '2':
	
	total2=total2+1;
	break;

case '3':

	total3=total3+1;
	break;

case '4':

	total4=total4+1;
	break;
	
case '5':

	total5=total5+1;
	break;

case '6':

	total6=total6+1;
	break;
	
default:
	break;
	}	

	cout<<dicenumber<<endl;
}



cout<<"Amount of 1: "<< total1<<endl;
cout<<"Amount of 2: "<< total2<<endl;
cout<<"Amount of 3: "<< total3<<endl;
cout<<"Amount of 4: "<< total4<<endl;
cout<<"Amount of 5: "<< total5<<endl;
cout<<"Amount of 6: "<< total6<<endl;

    return 0;
}
In your switch case, change from

case '1':

to

case 1:

and do the same for the other cases, otherwise your switch case only recognizes chars instead of ints as intended.
Last edited on
Change your cases to look for 1 not '1'. Use single quotes for char types, not integers.

Also - you can include the <ctime> library and add srand(time(0)) once at the beginning of the main function to seed the random function. Otherwise you'll get the same results every time you run this.
Wow that was FAST :)

Thank you very much !

And yeah, using srand() appears to be better.

Have a nice day
Topic archived. No new replies allowed.