can anybody fix my code. this is the problem:Tossing Coins for a Dollar For this
ID: 3837368 • Letter: C
Question
can anybody fix my code. this is the problem:Tossing Coins for a Dollar For this assignment, you will create a game program using the Coin class from Programming Challenge 12. 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 the simulated coins. When a coin is tossed, the value of the coins are added to your balance if they land heads-up. 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 $1 or more. If your balance is exactly $1, you win the game. You lose if your balance exceeds $1. You can choose to walk away with your earnings at any point. Turn in Coin.h and main.cpp.
this is my coin.h
#include "Coin.h"
Coin::Coin()
{
int randomNum = rand() % 2 + 1; // use random() % 2 +1 to get a mun from 1 to 2, I select two because it easier to let player tp win
if (randomNum == 1) //checking the randomNum display
{
sideup = "head"; // if it's head
}
else
sideup = "tail"; // if it's tails
}
Coin::~Coin()
{
}
void Coin::toss(){
int randomNum = rand() % 2 + 1; //generating a random number
if (randomNum == 1)
{
sideup = "head";
}
else
sideup = "tail";
}
std::string Coin::getsideup(){
return sideup;
}
this is my coin.cpp
it's not working , anybody can help me
Explanation / Answer
Hi, actually am giving you a new code..if its not a problem for u.
Here is Coin.h file
#include <iostream>
#include <cstdlib>
class Coin
{
double CoinValue;
public :
Coin()
{
CoinValue = 0;
}
Coin(Double value)
{
CoinValue = value;
}
bool coinTosseedValue()
{
return rand()%2;
}
double getCoinVale()
{
return CoinValue;
}
}
here is main file
#include "Coin.h"
#include<iostream>
using namespace std;
int main()
{
//The program should have three instances of class Coin one for dime,other for quarter and one for nickel
Coin quarter(0.25);
Coin dime(0.10);
Coin nickel(0.05);
//at the starting balance is 0;
double balance=0;
while(balance < 1)
{
//added balance respective to the type if the coin is heads up otherwise nothing will be added
if(quarter.coinTosseedValue())
{
balance+= quarter.getCoinVale();
}
if(dime.coinTosseedValue())
{
balance+= dime.getCoinVale();
}
if(nickel.coinTosseedValue())
{
balance+= nickel.getCoinVale();
}
}
if(balance ==1)
{
cout << "You won the game ...Yipee" <<endl;
}
else
{
cout <<"Alas,You lost the game"<<endl;
}
}
if any query,please revert
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.