Program: Heads or Tails? Counting results of 20 random coin tosses


Deliberately aimed at students and beginners. The emphasis here is on code that is easy to read. A much shorter version is possible! Point to prove: it's not just about the choice of programming language in schools and colleges. The style of code used in examples is often over-looked, making them far less useful to students.
As always, you can compile and run this on a Raspberry Pi, Apple Mac or Linux PC. Alternatively, paste the code into repl.it.

Code below:

#include <iostream>     // Library of functions for keyboard input and screen output
#include <cstdlib>      // Library for using pseudo-random number functions
#include <ctime>        // Library for time function, used to seed random number process

using namespace std;    // Makes use of cout and cin a little easier... shorthand :-)

// More than one way to solve this problem :-)

// This version makes a lot of if-then-else style constructs to make the code clearer for beginners.
// The same results could be achieved using arrays and without
// explicit comparisons to true etc. leading to a shorter program, less suited to beginners at GCSE

// - - - - - - - - - - - - - - - - - -

void showInstructions()
{
    cout << "Simulates the tossing of a coin 20 times," << endl;
    cout << "displaying H or T as appropriate" << endl << endl;
}

// - - - - - - - - - - - - - - - - - -

void playGame()
{
    int totalHeads = 0;
    int totalTails = 0;

    for ( int throwNum = 0;  throwNum < 20;  throwNum++ )
    {
        int result = random() % 2;

        cout << "It was ";

        if ( result == 0 )
        {
            cout << "heads." << endl;
            totalHeads++;
        }
        else
        {
            cout << "tails." << endl;
            totalTails++;
        }

    } // end of 20 throws

    // Display the results
    cout << endl;
    cout << "Heads: " << totalHeads << endl;
    cout << "Tails: " << totalTails << endl;
}

// - - - - - - - - - - - - - - - - - -

bool playAgain()
{
    cout << "Do you want to play another game? y or n ";
    char choice;
    cin >> choice;

    if ( choice == 'y'  or  choice == 'Y' )
        return true;
    else
        return false;
}

// - - - - - - - - - - - - - - - - - -

int main()
{
    // Seed the random number process, uses the system time as a starting point in a sequence of generated numbers
    srandom( time( 0 ) );


    showInstructions();

    // Main loop allowing many games to be played until you choose to quit
    do
    {
        playGame();

    } while ( playAgain() == true );

    cout << endl << "Laters. It's been real." << endl;
}