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;
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <ctime>
usingnamespace 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++;
elseif (num == 2)
num2++;
elseif (num == 3)
num3++;
elseif (num == 4)
num4++;
elseif (num == 5)
num5++;
elseif (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.
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.