Monday 15 March 2010

C++ Writing simple coin flip game -



C++ Writing simple coin flip game -

i'm writing code game prompts user pick how many times want flip coin , guess how many times land on heads. wrote of, need help finishing up. tried include count of heads ran problems.

#include <iostream> #include <cmath> #include <ctime> using namespace std; int myrandnumgen(){ int num = rand(); homecoming num; } char cointossfunction( ){ char cointoss; int cointossvalue = (myrandnumgen()%2); // 0 or 1 switch (cointossvalue) { case 0: cointoss = 'h'; break; case 1: cointoss = 't'; break; default: break; } homecoming cointoss; } int calccoin(int n){ int cout_heads=0; for(int i=0;i<=n;i++){ if(cointossfunction() == 'h') ++cout_heads; } homecoming (cout_heads/n); } int main(){ int coinflips, guess; cout << "how many times want flip coin? " << endl; cin >> coinflips; cout << "guess how many times coin land on heads if flipped: " << endl; cin >> guess; if (guess>coinflips) { cout << "guess error"; } for(int i=1;i<=coinflips;i++){ cout << calccoin; }

here few problems code:

for(int i=0;i<=n;i++)

this create i take values 0 n, means come in in loop n+1 times, instead of n times.

return (cout_heads/n);

since both variables cout_headsand n integers, perform integer division, , not floating point division. result 0 or 1 in case.

cout << calccoin;

when phone call function need set parenthesis. calcoin function takes parameter.

c++

No comments:

Post a Comment