I was assigned to write a program that will solve this problem:
--------------------------------------------------------------------------
Write a program that simulates coin tossing. For each toss of the coin the
program should print Heads or Tails. Let the program toss the coin 100 times, and count the number of times each side of the coin appears. Print the results. The program should call a separate function flip()that takes no arguments and returns 0 for tails and 1 for heads.
#include<iostream>
#include<cstdlib>
usingnamespace std;
int flip();
int main ()
{
int coin, counter, tails = 0, heads = 0;
for (counter = 1; counter <= 100; counter++)
{
coin = int flip ();
if(coin == 0)
{
cout<<"T ";
tails = tails + 1;
}
elseif( coin == 1)
{
cout<<"H ";
heads = heads + 1;
}
}
cout<<endl;
cout<<"Tails was tossed "<<tails<<" times"<<endl;
cout<<"Heads was tossed "<< heads<<" times"<<endl;
}
int flip()
{
return rand( ) % 2;
}
I keep getting the error "Error LNK1120: 1 unresolved externals." I don't what that is nor how to fix it. Can anyone tell me where I went wrong? Thank you!
This is u're code with minor change. You should include the conio.h so u can have access to the _getch() to stop the program.
And if u don't want the numbers to be the same each time, u should put a srand(time(NULL)); before the for. So it can generate new numbers every time