Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Code instructions:

The Coin class will act as the base class. Three child classes will be created using the Coin class as the base class. The classes will be Quarter, Dime, and Nickel. These child classes will contain a constructor that will be used to set the value field to the value of their amount (Quarter = 0.25, Dime=0.10, Nickel=0.05).

Instead of creating a separate counter variable to keep the balance of the coins the static field balance will keep track of the balance of the coin tosses. Write a program that uses the Quarter, Dime, and Nickel classes.

The program will create one of each instance of the Quarter, Dime, and Nickel class. When the game begins, your starting balance is $0. During each round of the game, the program will toss each of the simulated coins. 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.

In the main create a function called evaluateToss that will take a Coin reference pointer parameter. Use this method to toss the coin, decide if the coin is showing heads or tails, and add the value to the balance if needed. Pass the Quarter, Dime, and Nickel objects to this function each time to do the work.

What I have tried:

Below is the code I've written so far, but can anyone please help me to write the correct code by using a pointer reference parameter in the evaluateToss function so that it works correctly?

C++
#include<iostream>
#include<string>
#include<stdlib.h>

using namespace std;
//Coin class
class Coin {
private:
	double balance;
	string sideUp;
	bool heads;
	double value;

public:
	Coin(double val) {
		value = val;
		int side = rand() % 2;
		if (side == 0) {
			sideUp = "heads";
			heads = true;
		}
		else {
			sideUp = "tails";
			heads = false;
		}
	}

	void toss() {
		int side = rand() % 2;
		if (side == 0) {
			sideUp = "heads";
			heads = true;
		}
		else {
			sideUp = "tails";
			heads = false;
		}
	}

	void addBalance() {
		balance += value;
	}

	double getBalance() {
		return balance;
	}

	bool getHeads() {
		return heads;
	}

	double getValue() {
		return value;
	}

	string getSideUp() {
		return sideUp;
	}
};

//Dime class sets value
class Dime : public Coin { public: Dime(double val) : Coin(val) {} };


//Quarter class sets value
class Quarter : public Coin {
public:
	Quarter(double value) : Coin(value) {

	}
};

//Nickle class sets value
class Nickle : public Coin {
public:
	Nickle(double value) : Coin(value) {

	}
};


//evaluateToss method
void evaluateToss(Coin &coin) {
	coin.toss();
	if (coin.getHeads()==true) {
		coin.addBalance();
	}
	else {
		return;
	}
	std::cout << coin.getBalance();
	cout << "\n\n";
	if (coin.getBalance() == 1.0) {
		std::cout << "You win!!!";
	}
	else if (coin.getBalance() > 1.0) {
		std::cout << "You lose!!!";
	}
}
//main class
int main()
{
	std::cout << "Starting the Coin Toss Game";
	std::cout << "Ready? Set? Go!!!";

	Coin coin(0);
	coin.addBalance();
	Dime dime(0.10);
	Nickle nickle(0.05);
	Quarter quarter(0.25);

	while (coin.getBalance()<1.0) {
		evaluateToss(dime);
		evaluateToss(nickle);
		evaluateToss(quarter);
		}
	
}
Posted
Updated 4-Mar-22 9:16am
v2

You correctly pass the reference, but didn't use a static field (as explicitly required) for storing the balance. Moreover I suppose it's best to work with integers (using cents of dollar as unit of measure) instead of doubles.
Try
C++
#include<iostream>
#include<string>
#include<cstdlib>

using namespace std;

class Coin
{
  static int s_balance;
  int m_value;

public:
  Coin(int value) : m_value{value}{}

  void addBalance()
  {
    s_balance += m_value;
  }

  static double getBalance()
  {
    return s_balance;
  }

  bool landed_heads_up(){ return ((rand() % 2) == 1); }
};

int Coin::s_balance = 0;

class Dime: public Coin
{
public:
  Dime():Coin(10){}
};

class Quarter: public Coin
{
public:
  Quarter():Coin(25){}
};

class Nickle: public Coin
{
public:
  Nickle():Coin(5){}
};

//evaluateToss function
int evaluateToss(Coin &coin)
{
  int balance = Coin::getBalance();

  if ( coin.landed_heads_up())
  {
    coin.addBalance();
    balance =  Coin::getBalance();

    std::cout << balance;
    cout << "\n\n";

    if (balance == 100)
    {
      std::cout << "You win!!!";
    }
    else if (balance > 100)
    {
      std::cout << "You lose!!!";
    }
  }
  return balance;
}

int main()
{
  srand((unsigned) time(NULL));
  std::cout << "Starting the Coin Toss Game";
  std::cout << "Ready? Set? Go!!!";

  Dime dime;
  Nickle nickle;
  Quarter quarter;

  while (true)
  {
    if ( evaluateToss(dime) >= 100) break;
    if ( evaluateToss(nickle) >= 100) break;
    if ( evaluateToss(quarter) >= 100) break;;
  }
}
 
Share this answer
 
Compiling does not mean your code is right! :laugh:
Think of the development process as writing an email: compiling successfully means that you wrote the email in the right language - English, rather than German for example - not that the email contained the message you wanted to send.

So now you enter the second stage of development (in reality it's the fourth or fifth, but you'll come to the earlier stages later): Testing and Debugging.

Start by looking at what it does do, and how that differs from what you wanted. This is important, because it give you information as to why it's doing it. For example, if a program is intended to let the user enter a number and it doubles it and prints the answer, then if the input / output was like this:
Input   Expected output    Actual output
  1            2                 1
  2            4                 4
  3            6                 9
  4            8                16
Then it's fairly obvious that the problem is with the bit which doubles it - it's not adding itself to itself, or multiplying it by 2, it's multiplying it by itself and returning the square of the input.
So with that, you can look at the code and it's obvious that it's somewhere here:
C++
int Double(int value)
   {
   return value * value;
   }

Once you have an idea what might be going wrong, start using the debugger to find out why. Put a breakpoint on the first line of the method, and run your app. When it reaches the breakpoint, the debugger will stop, and hand control over to you. You can now run your code line-by-line (called "single stepping") and look at (or even change) variable contents as necessary (heck, you can even change the code and try again if you need to).
Think about what each line in the code should do before you execute it, and compare that to what it actually did when you use the "Step over" button to execute each line in turn. Did it do what you expect? If so, move on to the next line.
If not, why not? How does it differ?
Hopefully, that should help you locate which part of that code has a problem, and what the problem is.
This is a skill, and it's one which is well worth developing as it helps you in the real world as well as in development. And like all skills, it only improves by use!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900