I am trying to write a program that will flip a coin 100 times and count the number of times it lands on heads as well as the number of times it lands on tails. I have a function, flip, that returns 0 for tails and 1 for heads. When I run the program, the output I get is 2 for heads and 99 for tails. I can't figure out where I went wrong in my code.
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
using std::cout;
using std::rand;
int flip();
int main()
{
int coinToss;
int heads, tails;
heads = 1;
tails = 0;
coinToss = 100;
for (int toss = 0; toss < coinToss; toss++)
{
flip();
if (toss == 1)
heads = heads + 1;
else
tails = tails + 1;
}
cout << "Heads appear " << heads << " times.\n";
cout << "Tails appear " << tails << " times.\n";
return 0;
}
int flip()
{
return rand() % 2;
}