call function with a loop

Hello, so for my homework i am supposed to create a coin toss stimulator. The output should should the number of flips the user chose to enter, number of heads and tails and the percentage. Here is my code below, i cannot get the number of heads and tails to be right, everytime i test it out, the number of heads and tails becomes 0, which makes the percentage 0 as well. please help! thanks!
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869`` `````` #include #include #include #include using namespace std; unsigned short coinToss(); int main() { const int heads = 1, tails = 2; int flips, total=0, totalHeads = 0, totalTails = 0, final; char quit; cout << "Hello and welcome to the Coin Toss Stimulator." << endl; do { cout << "How many times do you wish to flip the coin?"; cin >> flips; //If they choose a number less than 0 if (flips <= 0) cout << "\nPlease enter a number greater than 0."; //If they choose a number greater than 0 if (flips > total) { final = coinToss(); if (final == heads) totalHeads = totalHeads + 1; else if (final == tails) totalTails = totalTails + 1; total++; } // calculations for percent double percentHeads = (totalHeads/flips), percentTails = (totalTails/flips); //stats cout << "\nCoin Toss Statistics" << endl; cout << "_____________________" << endl; cout << "Number of coin tosses: " << flips << endl; cout << "Number of heads: " << totalHeads << endl; cout << "Percentage of heads: " << percentHeads*100 << "%" << endl; cout << "Numebr of tails: " << totalTails << endl; cout << "Percentage of tails: " << percentTails*100 << "%" << endl; //Asks if they would like to run the program again cout << "If you would wish to end this program, press Q."; cin >> quit; }while (quit != 'q' && quit != 'Q'); return 0; } unsigned short coinToss() { unsigned seed = unsigned (time(0)); srand(seed); unsigned short toss = (rand()%2)+1; return toss; } ``````
Try something like this. You don't need to use srand() before getting each next value. It only uses to initialize PRN-generator before start current sequence of numbers.

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950`` ``````#include #include using namespace std; bool coinToss(); int main(int argc, char* argv[]) { //initialize pseudo-random number generator time_t seed = time(NULL); srand(seed); int flipsNum = 0; char finish; while(true) { int totalHeads = 0; int totalTails = 0; cout << "How many times do you wish to flip the coin?" << endl; cin >> flipsNum; for(int i = 0; i < flipsNum; ++i) { if (coinToss() ) { totalHeads++; } else { totalTails++; } if (i == flipsNum - 1) { cout << double(totalHeads) / flipsNum << endl << double(totalTails) / flipsNum << endl; } } cout << "One more? Or press Q or q to exit" << endl; cin >> finish; if (finish == 'Q' || finish == 'q') break; } return 0; } bool coinToss() { return rand() % 2; }``````
Last edited on
a) If you divide an integer by an other integer, you’re performing an integer division.
It means it returns an integer, i.e. a number which can be 0 or 1 but not between 0 and 1. If the result is between 0 and 1, it will become 0.

b) ‘final’ is a reserved word. You can’t have a variable named ‘final’.

c) Declaring variables just before using them is better than declaring them all together at the beginning of a function.

d) Simple code it’s often the best code ;-)

Hints:
 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061`` ``````#include #include #include int coinToss(); int main() { srand(time(0)); char quit {}; std::cout << "Hello and welcome to the Coin Toss Stimulator.\n"; do { int flips = 0; do { std::cout << "How many times do you wish to flip the coin? "; std::cin >> flips; if (flips <= 0) { std::cout << "Please enter a number greater than 0.\n"; } } while(flips < 1); constexpr int HEAD = 1; int total = 0, totalHeads = 0, totalTails = 0; while (flips > total) { int chance = coinToss(); if (chance == HEAD) { totalHeads++; } else { totalTails++; } total++; } // calculations for percent double percentHeads = static_cast(totalHeads) / flips, percentTails = static_cast(totalTails) / flips; //stats std::cout << "\nCoin Toss Statistics" "\n_____________________" "\nNumber of coin tosses: " << flips << "\nNumber of heads: " << totalHeads << "\nPercentage of heads: " << percentHeads * 100 << '%' << "\nNumber of tails: " << totalTails << "\nPercentage of tails: " << percentTails * 100 << "%\n"; //Asks if they would like to run the program again std::cout << "If you would wish to end this program, press Q. "; std::cin >> quit; } while (quit != 'q' && quit != 'Q'); return 0; } int coinToss() { int toss = rand() % 2 ; // if even returns 0, otherwise returns 1 return toss; }``````

Last edited on