coin toss problem issue

Im working on this problem..
--------------------------------

Write a function named cointoss that simulates the tossing of a coin.

When you call the function, it should generate a random number in the range of 1 through 2.

If the random number is 1, the function should display "heads".

If the random number is 2, the function should display "tails".

Demonstrate the function in a program that asks the user how many times the coin should be tossed, and then simulates tossing the coin that number of times.

Report the total number of heads and tails.

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

no compiler issues but everytime i run it, it either gives me all heads or all tails..

for example

How many tosses should I make?
7
heads
heads
heads
heads
heads
heads
heads
heads
Press any key to continue . . .


how can i fix that to where its not all heads or all tails?

thanks in advance!

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
  #include<iostream>
#include<ctime>
#include<cstdlib>

using namespace std;

void coinToss();

int main()
{   int times;
cout<< "How many times the coin should be tossed?" << endl;
cin>> times;

for(int i=1;i<=times;i++)
{
coinToss();
}

system("pause");
return 0;
}

void coinToss()
{
int randomNumber;
unsigned seed=time(0);
srand(seed);
randomNumber=rand()%2+1;
if(randomNumber==1)cout<< "heads" << endl;
else if (randomNumber==2)cout<<"tails" <<endl;

}
I redid it and it works for me
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
 #include<iostream>
#include<ctime>
#include<cstdlib>

using namespace std;

void coinToss();

int main()
{   
    srand(static_cast<unsigned int>(time(0)));
    int times;
    cout<< "How many times the coin should be tossed?" << endl;
    cin>> times;

    for(int i=1;i<=times;i++)
    {
    coinToss();
    }

return 0;
}

void coinToss()
{
    int randomNumber;
    randomNumber=rand()%2+1;
    if(randomNumber==1)cout<< "heads" << endl;
    else if (randomNumber==2)cout<<"tails" <<endl;
}
thanks!
Topic archived. No new replies allowed.