Question: code in C++ Output should look like this! Output Your current balance is $0.00 Nickel: tails Dime: tails Quarter: tails ----------------------------- Your current balance is
code in C++
Output should look like this!
Output
Your current balance is $0.00 Nickel: tails Dime: tails Quarter: tails ----------------------------- Your current balance is $0.00 Nickel: heads Dime: tails Quarter: heads ----------------------------- Your current balance is $0.30 Nickel: tails Dime: heads Quarter: tails ----------------------------- Your current balance is $0.40 Nickel: heads Dime: heads Quarter: tails ----------------------------- Your current balance is $0.55 Nickel: heads Dime: tails Quarter: heads ----------------------------- Your current balance is $0.85 Nickel: heads Dime: tails Quarter: heads ----------------------------- Your final balance is $1.15. You lost
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include
#include
#include
#include
using namespace std;
//Put the Coin class definition after this line
class Coin
{
private:
double value;
char side[6];
public:
Coin();
Coin(double val);
void toss();
int getSide();
double getValue();
};
int main()
{
//Set the random number generator and the formatting for the output
srand( 1 );
cout << fixed << setprecision(2);
//Create objects using the two constructors
Coin coin1;
Coin coin2( 0.25 );
Coin coin3( 0.10 );
//Test 1: the getSide method
cout << "Test 1: use the getSide method on all of the objects" << endl << endl
<< "coin1 has " << ( (coin1.getSide() == 1)? "heads" : "tails" ) << " up" << endl
<< "coin2 has " << ( (coin2.getSide() == 1)? "heads" : "tails" ) << " up" << endl
<< "coin3 has " << ( (coin3.getSide() == 1)? "heads" : "tails" ) << " up" << endl;
//Test 2: the getValue method
cout << endl << endl
<< "Test 2: use the getValue method on all of the objects" << endl << endl
<< "coin1 has the value $" << coin1.getValue() << endl
<< "coin2 has the value $" << coin2.getValue() << endl
<< "coin3 has the value $" << coin3.getValue() << endl;
//Test 3: the toss method
cout << endl << endl
<< "Test 3: use the toss method 5 times on all of the objects" << endl << endl;
cout << "coin1 coin2 coin3" << endl
<< "-----------------------------" << endl;
for( int cnt = 1; cnt <= 5; cnt++ )
{
coin1.toss();
cout << ( (coin1.getSide() == 1)? "heads" : "tails" );
coin2.toss();
cout << ( (coin2.getSide() == 1)? " heads" : " tails" );
coin3.toss();
cout << ( (coin3.getSide() == 1)? " heads" : " tails" ) << endl;
}
return 0;
}
//Code the constructors and methods for the Coin class after this line
Coin::Coin()
{
value = 0.01;
toss();
}
Coin::Coin(double val)
{
value = val;
toss();
}
void Coin::toss()
{
int v = 1 + rand() % 2;
if(v == 1)
strcpy(side, "heads");
else
strcpy(side, "tails");
}
int Coin::getSide()
{
if(strcmp(side, "heads") == 0)
return 1;
else
return 2;
}
double Coin::getValue()
{
return value;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
