Coin flipping problem

Hello everyone,

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.

---------------------------------------------------------------------------

Here's the program I have written so far.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include<iostream>
#include<cstdlib>
using namespace 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;
        }
        else if( 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!
Hi there!
at line 12, you don't have to write the type of the function, omit the 'int' and that should do it.
Last edited on
Thank you for the prompt reply, but it still is showing the error after making the change you suggested :(
i tried the above code without the int and it worked for me.

what compiler are you using?
I'm using visual studios 2012 express.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include<iostream>
#include<cstdlib>
#include <conio.h>
using namespace std;

int flip();

int main ()
{
    int coin, counter, tails = 0, heads = 0;
    for (counter = 1; counter <= 100; counter++)
    {
        coin =  flip ();
 
        if(coin == 0)
        {
            cout<<"T ";
            tails = tails + 1;
        }
        else if( coin == 1) 
        {
            cout<<"H ";
            heads = heads + 1;
        }
    }
 
    cout<<endl;
    cout<<"Tails was tossed "<<tails<<" times"<<endl;
    cout<<"Heads was tossed "<< heads<<" times"<<endl;
 
 _getch();
}
int flip()
{
    return rand( ) % 2;
	_getch();
	
}


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
Last edited on
Topic archived. No new replies allowed.