### 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.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748`` ``````#include #include #include #include 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 <

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:
 ``1234567891011`` ``````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

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647`` ``````#include #include #include 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.