Dice Rolls Problem assistance

Hello!!!

I have started to learn the C++ language and right now I am confused as to why my program is not running.It is about the random generation of dice rolls and how many times it lands on 1 to 6 and loop should be used. If anyone can help this is what i wrote and its not running. Thanks.

#include <cstdlib>
#include <iostream>
#include <cmath>
#include <ctime>
using namespace std;
int main()
{
srand (time (NULL));
//counter statement 1200 trials
int counter =0;
//integer variables
int num1=0,num2=0,num3=0,num4=0,num5=0,num6=0,num;
//loop statement for sides of die
while (counter <= 1200);


}
num =getRand()%6 + 1;
cout << num <<endl;
//else if statements

if(num == 1)
num1++;
else if (num == 2)
num2++;
else if (num == 3)
num3++;
else if (num == 4)
num4++;
else if (num == 5)
num5++;
else if (num == 6)
num6++;
counter=counter+1;
{
//results will return the amount of 1's to 6's randomly generated
cout << num <<endl;
cout <<"1"<<num1<<endl;
cout <<"2"<<num2<<endl;
cout <<"3"<<num3<<endl;
cout <<"4"<<num4<<endl;
cout <<"5"<<num5<<endl;
cout <<"6"<<num6<<endl;


system("PAUSE");
return EXIT_SUCCESS;
}
Last edited on
When you say won't run, do you mean won't compile?

I've put your code into a <> code block, so it's easier to read.

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
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <ctime>
using namespace std;
int main()
{
srand (time (NULL));
//counter statement 1200 trials
int counter =0;
//integer variables
int num1=0,num2=0,num3=0,num4=0,num5=0,num6=0,num;
//loop statement for sides of die
while (counter <= 1200);


}
num =getRand()%6 + 1;
cout << num <<endl;
//else if statements

if(num == 1)
num1++;
else if (num == 2)
num2++;
else if (num == 3) 
num3++; 
else if (num == 4)
num4++;
else if (num == 5)
num5++;
else if (num == 6)
num6++;
counter=counter+1;
{
//results will return the amount of 1's to 6's randomly generated
cout << num <<endl;
cout <<"1"<<num1<<endl;
cout <<"2"<<num2<<endl; 
cout <<"3"<<num3<<endl;
cout <<"4"<<num4<<endl;
cout <<"5"<<num5<<endl;
cout <<"6"<<num6<<endl;


system("PAUSE");
return EXIT_SUCCESS;
}


You have a couple of problems ...

1. You have an infinite loop on line 14. counter is set to zero on line 10, and your while loop repeats while it's <= 1200. The loop is also empty - you have a closing semi-colon after it, which defines an empty body.

2. Line 17 is ending the main() function - the corresponding open brace is line 7, which is the beginning of main()'s body.

You need to check all your braces match up.

Do that and if you still have problems, repost your code (with <> tags) and we'll go from there.

Jim
The code is pretty hard to read without proper indentation.
However, this looks like the main function:
1
2
3
4
5
6
7
8
9
10
11
int main()
{
    srand (time (NULL));
    //counter statement 1200 trials
    int counter =0;
    //integer variables
    int num1=0,num2=0,num3=0,num4=0,num5=0,num6=0,num;
    //loop statement for sides of die
    while (counter <= 1200)
        ;
 }


There is a while loop at line 9 which repeatedly executes the empty statement at line 10.

The rest of the code which follows is outside the function and plays no part at all.
1) You have the { and } reversed for your while loop.
2) The ; doesn't belong after the while.
3) The correct call is rand(), not getRand()

Here's what it should probably look like

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
#include <cstdlib>
#include <iostream>
#include <ctime>

using namespace std;

int main()
{
	srand(time(0));
	
	int counter = 0;
	int num1 = 0, num2 = 0, num3 = 0, num4 = 0, num5 = 0, num6 = 0, num;
	
	while (counter <= 1200)
	{	
		num = rand() % 6 + 1;
		cout << num << endl;

		if(num == 1)
		    num1++;
		else if (num == 2)
		    num2++;
		else if (num == 3)
		    num3++;
		else if (num == 4)
		    num4++;
		else if (num == 5)
		    num5++;
		else
		    num6++;
			
		counter++;
	}
	
	cout << num << endl;
	cout << "1 " << num1 << endl;
	cout << "2 " << num2 << endl;
	cout << "3 " << num3 << endl;
	cout << "4 " << num4 << endl;
	cout << "5 " << num5 << endl;
	cout << "6 " << num6 << endl;


	cout << "Press Enter to close." << endl;
	cin.ignore(99, '\n');
	return 0;
} 
Last edited on
I have made the changes you suggested and now the program compiles. Your help was greatly appreciated
!! thank you!!
Topic archived. No new replies allowed.