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!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
  #include <iostream>
#include <cstdlib>
#include <ctime>
#include <cmath>
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <ctime> 

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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <cstdlib>
#include <ctime>
#include <iostream>

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<double>(totalHeads) / flips,
               percentTails = static_cast<double>(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
Topic archived. No new replies allowed.