I had to write a C++ program for school and I got it work, but I cant figure out how to get it to keep the values to display the amount of times heads was tossed and tails was tossed. It keeps saying 0. These are the directions:Write a C++ program that simulates coin tossing. For each toss of the coin the program should print Heads or Tails. The program should toss a coin 100 times. Count the number of times each side of the coin appears and print the results at the end of the 100 tosses.
The program should have the following functions as a minimum:
void toss() - called from main() and will randomly toss the coin and set a variable equal to the face of the coin void count() - called from toss to increment counter for heads or tails void displayCount() - is called from main() and will display the values of the counter for heads and the counter for tails.NO GLoBAL VARIABLES!!
Following is the code:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
//prototypes
void toss(int headCount, int tailCount);
void count(int headCount, int tailCount);
void displayCount(int headCount, int tailCount);
//tosses a coin 100 itmes and tells you how many were heads and how many were tails.
int main()
{
int headCount = 0;
int tailCount = 0;
int count = 0;
toss(headCount, tailCount); //toss coin
displayCount(headCount, tailCount); //displays how many heads and tails
}
//***************function definitions***************
void toss(int headCount, int tailCount)
{
srand(time(NULL));//makes the coin toss random
for (int i = 0; i<100; i++) //while it is less then 100 tosses
{
if (rand() % 2 == 0) //assigns 0 to heads
{
cout << "heads";
}
else
{
cout << "tails"; //assigns 1 to tails
}
}
count(headCount, tailCount);
}
void count(int headCount, int tailCount)
{
if (rand() % 2 == 0)
{
headCount++; //counts the amount of heads
}
else
{
tailCount++; //counts the amount of tails
}
}
void displayCount(int headCount, int tailCount) //displays count of head and tails
{
cout << "Number of heads: " << headCount << "\n";
cout << "Number of tails: " << tailCount << "\n";
}