For this assignment, you will create a game program using the Coin class from Pr
ID: 3832466 • Letter: F
Question
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.
Explanation / Answer
// Coin.h
#include <string>
using namespace std;
class Coin {
public:
string sideup;
Coin()
{
int randomNum = rand() % 10 + 1;
if(randomNum==1)
{
sideup="head";
}
else
sideup="tail";
}
void Toss()
{
int randomNum = rand() % 10 + 1;
if(randomNum==1)
{
sideup="head";
}
else
sideup="tail";
}
string getsideup()
{
return sideup;
}
};
// main.cpp
#include <cstdlib>
#include <iostream>
#include <cstring>
#include "Coin.h"
using namespace std;
int main()
{
Coin quarter;
Coin dime;
Coin nickle;
double balance=0;
for(int i=0;i<20;i++)
{
quarter.Toss();
dime.Toss();
nickle.Toss();
cout<<quarter.getsideup()<<endl;
if(quarter.getsideup()=="head")
{
balance=balance+0.25;
}
cout<<dime.getsideup()<<endl;
if(dime.getsideup()=="head")
{
balance=balance+0.1;
}
cout<<nickle.getsideup()<<endl;
if(nickle.getsideup()=="head")
{
balance=balance+0.05;
}
if(balance>=1)
break;
}
if(balance==1)
{
cout<<"you Won"<<endl;
}
else
cout<<"You lose"<<endl;
return 0;
}
/*
output:
tail
tail
tail
tail
tail
tail
tail
tail
tail
head
tail
tail
tail
head
tail
tail
tail
tail
tail
tail
tail
tail
head
tail
tail
tail
tail
tail
tail
tail
tail
tail
tail
tail
tail
tail
tail
tail
tail
tail
tail
tail
tail
tail
tail
tail
head
tail
tail
tail
head
tail
tail
tail
head
tail
tail
tail
tail
tail
You lose
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.