Coin toss simulator

I have most of the code down, just this last part where I need to report the total number of heads and tails. I feel like this is very simple but i'm having the biggest brain fart, any amount of help will do. Thanks!
Here is the lab assignment -


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.


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

using namespace std;
int coinToss (void);
{
    int randomNumber;
    randomNumber = 1 + rand() % 2;
    return randomNumber;
}

int main()
{

    int howManyTimes = 0;
    int randomNumber = 0;
    string headTail = "";

    cout << "How many times do you want to toss the coin? ";
    cin >> howManyTimes;

    srand((time(0)));
    for (int i = 1; i <= howManyTimes; i++)
    {
        randomNumber = coinToss();
        if (randomNumber == 1)
            headTail = "Head.";
        else
            headTail = "Tail.";

        cout << headTail << endl;
    }
    return 0;
}
If you keep track of heads, then tails will be howManyTimes - heads, and vice-versa.
I'm still a bit confused, could you elaborate on how to keep track of heads?
Set up a count variable for when randomNumber == 1 and a separate count for when randomNumber==2. Then simply print the counts onto the screen.
Topic archived. No new replies allowed.