c++ - using classes to create a coin toss program -
having trouble figuring out constructors code in main() supposed test class can go on rest of program. instructions constructors
this class has 2 constructors. default constructor (the 1 takes no arguments) should initialize value of coin penny (0.01) , side should initialized calling toss() method described below.
the other constructor takes 1 argument: double holds initial value coin. passed in argument should used initialize value of coin. no error checking required. side should initialized calling toss() method described below.
this code
#include <iostream> #include <iomanip> #include <cstdlib> #include <ctime> #include <cstring> using namespace std; //put coin class definition after line class coin { public: coin() { value = 0.01; toss(); } coin(double ) { toss(); } void toss(); int getside(); double getvalue(); private: double value; char side[6]; }; int main() { //set random number generator , formatting output srand( 1 ); cout << fixed << setprecision(2); //create objects using 2 constructors coin coin1; coin coin2( 0.25 ); coin coin3( 0.10 ); //test 1: getside method cout << "test 1: use getside method on of 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: getvalue method cout << endl << endl << "test 2: use getvalue method on of objects" << endl << endl << "coin1 has value $" << coin1.getvalue() << endl << "coin2 has value $" << coin2.getvalue() << endl << "coin3 has value $" << coin3.getvalue() << endl; //test 3: toss method cout << endl << endl << "test 3: use toss method 5 times on of 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 constructors , methods coin class after line void coin::toss() { int num = rand() % 2+ 1; if (num == 1) { strcpy(side,"heads"); } else { strcpy(side,"tails"); } } int coin::getside() { if (side=="heads") { return 1; } else { return 2; } } double coin::getvalue() { return value; }
finally figured out gave effort!
#include <iostream> #include <iomanip> #include <cstdlib> #include <ctime> #include <cstring> using namespace std; //put coin class definition after line class coin { public: coin(); coin(double ); void toss(); int getside(); double getvalue(); private: double value; char side[6]; }; int main() { //set random number generator , formatting output srand( time(null) ); cout << fixed << setprecision(2); //create objects using 2 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 << "-----------------------------" <<endl << "your current balance $"<< balance <<endl << endl; } cout<< "the final balance "<< balance<<" congrats! won!"<<endl; return 0; } //code constructors , methods coin class after 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; }