Confused with loops

I'm having trouble getting the program to calculate the percentage of times it flipped either heads versus tails. I realize what I currently have doesn't work because above I initialized heads and tails by giving them values of 0, but I'm unsure of how to fix it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main ()
{	
	srand(time(0));
	int coin, heads = 0, tails = 0;
	float percent_heads, percent_tails;
	string heads_tails;
	for (int count = 0; count < 10; count ++)
	{
		coin = rand() % 2; 
		if (coin == 1)
			heads++;
		else
			tails++;		
	}	
	percent_heads = heads / 10;
	percent_tails = tails / 10;
	cout << "The number of heads = " << heads << ", percent = " << percent_heads << endl;
	cout << "The number of tails = " << tails << ", percent = " << percent_tails << endl;	
}
Last edited on
I realize what I currently have doesn't work because above I initialized heads and tails by giving them values of 0, but I'm unsure of how to fix it.

It doesn't work because you're using integer division and that's not what you want. Change 10 to 10.0 on lines 20 and 21.
Thanks!
Topic archived. No new replies allowed.