faces of dice

I am learning programming language first time, need help with compile the program.
Using the random number generation, I need to get the out put like this:

How many times you want to throw the dice (0 or less will stop the program)? 40
Face Frequency Percentage
-----------------------------------
1 8 20.0%
2 5 12.5%
3 6 15.0%
4 8 20.0%
5 5 12.5%
6 8 20.0%
-----------------------------------
Total 40 100.0%
Face 1: ********(8)
Face 2: *****(5)
Face 3: ******(6)
Face 4: ********(8)
Face 5: *****(5)
Face 6: ********(8)
How many times you want to throw the dice (0 or less will stop the program)?


I am using while loop and switch case, please guide me.

{
int num=0;
int i;
int face;
int frequency1 = 0;
int frequency2 = 0;
int frequency3 = 0;
int frequency4 = 0;
int frequency5 = 0;
int frequency6 = 0;
cout<< "How many times you want to throw the dice ( 0 or less will stop the program)? ";
cin>> num;
cout<<endl;
for(i = 1; 1<=40; i++)
{
face = 1+rand()%6;
switch (face)
{
case 1: ++frequency1;
break;
case 2: ++frequency2;
break;
case 3: ++frequency3;
break;
case 4: ++frequency4;
break;
case 5: ++frequency5;
break;
case 6: ++frequency6;
break;
}
}
return 0;
}
Hi ummer,

Please use code tags (the <> button) to surround code, some indenting will also make it easier to read (for the people trying to help you, at least).

I assume your code starts with:
1
2
3
4
#include <iostream>
using namespace std;

int main()


Other than that, looks pretty good.

1
2
3
cin>> num;
cout<<endl;
for(i = 1; 1<=40; i++)  // replace 40 with num 


At this point, the frequency variables are the amount that that number was rolled, so you just need some math to figure out the percentage.
This line is incorrect: for(i = 1; 1<=40; i++)
(a) you have 1<=40 .... should be i <= 40.
(b) i <= 40 should be i <= num


I'd also suggest the use of an array int frequency[6] rather than separate individual counts.

Then instead of switch/case. you could have this:
1
2
3
4
5
    for (int i=0; i<num; i++)
    {
        int face = 1+rand()%6;
        ++frequency [face-1];
    }





Last edited on
Topic archived. No new replies allowed.