Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ programming Tossing Coins for a Dollar For this part, you will create a game

ID: 3666232 • Letter: C

Question

C++ programming

Tossing Coins for a Dollar

For this part, 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 coin is added to your balance if it lands 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.

Explanation / Answer

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <cstring>

using namespace std;

//Put the Coin class definition after this line

class Coin
{
public:
Coin();
Coin(double );
void toss();

int getSide();
double getValue();

private:
double value;   
char side[6];
};

int main()
{
//Set the random number generator and the formatting for the output

srand( time(NULL) );

cout << fixed << setprecision(2);


//Create objects using the two constructors

Coin coin1;
Coin coin2( 0.25 );
Coin coin3( 0.10 );


double balance;

while (balance < 1.00)

{
cout << "current Balance: "<<balance<<endl;

coin1.toss();
cout<< "Nickel: "<< ( (coin1.getSide() == 1)? "heads" : "tails" )<<endl;
if (coin1.getSide()==1)
{
balance +=coin1.getValue();

}

coin3.toss();
cout<< "Dime: " << ( (coin3.getSide() == 1)? "heads" : "tails" )<<endl;
if (coin3.getSide()==1)
{
balance +=coin3.getValue();

}
coin2.toss();
cout<< "Quarter: " << ( (coin2.getSide() == 1)? "heads" : "tails" )<<endl;
if (coin2.getSide()==1)
{
balance +=coin2.getValue();

}
cout << "your current balance is $"<< balance <<endl
<< endl;

}


cout<< "The final balance is "<< balance<<" Congrats! You won!"<<endl;


return 0;


}

//Code the constructors and methods for the Coin class after this line

void Coin::toss()

{

int num = rand() % 2+ 1;
if (num == 1)
{

strcpy(side,"heads");
}
else
{

strcpy(side,"tails");
}
}
int Coin::getSide()
{

if (strcmp(side,"heads")==true)
{
return 1;
}
else
return 2;
}


double Coin::getValue()
{
return value;


}

Coin::Coin()
{
value = 0.05;
toss();

}

Coin::Coin(double newValue)
{
value = newValue;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote