Question: Having trouble finishing this program. I need to write a class named Coin. The Coin class should have the following member variables: A string named

Having trouble finishing this program. I need to write a class named Coin. The Coin class should have the following member variables:

A string named sideUp. The sideUp member variables will hold either HEADS or tails indicating the side of the code that is facing up.

A boolean name heads. The heads member variable will be true if the heads side is up and false if the heads side is down.

A double named value. The value member will hold the decimal value of the coin.

The Coin class should have the following member functions:

A constructor that takes the value of the Coin object as a parameter and randomly decides the side of the coin that is facing up.

A void member function named toss that simulates the tossing of the coin. When the function is called it randomly determines the side of the coin that is facing up.

A boolean member function getHeads that returns the value of the heads variable.

A double member function getValues that returns the value of the coin value variable.

A string member function getSideUp that returns the value of either HEADS . or tails

Write a program that uses the Coin class. The program should have three instances of the Coin class: one representing a quarter, one representing a dime, and one representing a nickel. When the game begins, your starting balance is $0. During each round of the game, the program will toss each of the simulated coins at one time. When a tossed coin lands heads-up, the value of the coin is added to your balance. For example, if the quarter lands heads-up, 25 cents is added to your balance. Nothing is added to your balance for coins that land tails-up. The game is over when your balance reaches one dollar or more. If your balance is exactly one dollar, you win the game. If your balance exceeds one dollar, you lose.

The problem I'm having is that it doesn't seem to be randomizing heads and tails. When I run the program I will sometimes get something like 140 rounds of tails followed by 3 of heads. If it's truly 50/50 this shouldn't be happening. Here is my code:

#include #include #include

using namespace std;

class Coin { private: string sideUp; bool heads; double value;

public: Coin(double _cents = 5, bool _heads = true) { if (_heads) { sideUp = "HEADS"; } else { sideUp = "TAILS"; } heads = _heads; value = _cents * 0.01; }

void toss() { srand(time(0)); int random = rand() % 2; heads = random;

if (heads == 1) { sideUp = "HEADS"; } else { sideUp = "TAILS"; } }

bool getHeads() { return heads; }

double getValues() { return value; }

string getSideUp() { return sideUp; } };

#include "Coin.h"

int main() { Coin quarter(25), dime(10), nickel(5); double balance = 0; double add_value;

while (balance < 1) { quarter.toss(); cout << quarter.getSideUp() << ": "; add_value = quarter.getValues() * quarter.getHeads(); balance += add_value; cout << "Balance = " << balance << " " << endl;

if (balance >= 1) { break; }

dime.toss(); cout << dime.getSideUp() << ": "; add_value = dime.getValues() * dime.getHeads(); balance += add_value; cout << "Balance = " << balance << " " << endl;

if (balance >= 1) { break; }

nickel.toss(); cout << nickel.getSideUp() << ": "; add_value = nickel.getValues() * nickel.getHeads(); balance += add_value; cout << "Balance = " << balance << " " << endl;

if (balance >= 1) { break; } }

if (balance > 1) { cout << "You lost the game. Balance = " << balance << endl; } else { cout << "You won the game! Balance = " << balance << endl; }

return 0; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!