How to do I include Garbage Collection?

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
#include <string>
#include <iostream>




using namespace std;
    int main ()
    {
       

        int heads =0, tails = 0;
        int coin;
        string repeat;

        do
        {

        std::cout << "Please enter the number of tosses ";
        int tosses;
        std::cin >> tosses;
        for (int i = 0; i != tosses; i++)
        {
            coin = rand() % 2;


            switch (coin)
            {
                case 1:
                    heads++;
                    break;
                case 0:
                    tails++;
                    break;

            }
        }
        double percent = heads / (tosses * 1.0) * 100;
        std::cout << "Number of heads "  <<heads << std::endl;
        std::cout << "Number of tails "  <<tails << std::endl;
        std::cout << "Percentage of heads is  "<<  percent <<"%" << std::endl;
        std::cout << "Enter 'y' to play again, 'n' to quit: " << std::endl;
        std::cin>>repeat;
        }
        while (repeat == "y");


    }


OUTPUT:
      

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Please enter the number of tosses 34
Number of heads 17
Number of tails 17
Percentage of heads is  50%
Enter 'y' to play again, 'n' to quit:
y
Please enter the number of tosses 35
Number of heads 37
Number of tails 32
Percentage of heads is  105.714%
Enter 'y' to play again, 'n' to quit:
y
Please enter the number of tosses 3
Number of heads 39
Number of tails 33
Percentage of heads is  1300%
Enter 'y' to play again, 'n' to quit:
y
Please enter the number of tosses 1
Number of heads 39
Number of tails 34
Percentage of heads is  3900%
Enter 'y' to play again, 'n' to quit:
n
Last edited on
Since you use namespace std you can omit the std:: prefix for cout, cin and endl. Also use single quotes for single characters -> while (repeat == 'y');. To fix the problem with percent, set heads and tails to zero before the toss loop.
Thanx Revised code...

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
#include <string>
#include <iostream>
#include <cstdlib>

using namespace std;
    int main ()
    {
       

        
        int coin;
        string repeat;

        do
        {

        std::cout << "Please enter the number of tosses ";
        int tosses;
	int heads = 0, tails = 0;
        std::cin >> tosses;
        for (int i = 0; i != tosses; i++)
        {
            coin = rand()%2;


            switch (coin)
            {
                case 1:
                    heads++;
                    break;
                case 0:
                    tails++;
                    break;

            }
        }
        double percent = heads / (tosses * 1.0) * 100;
        std::cout << "Number of heads "  <<heads << std::endl;
        std::cout << "Number of tails "  <<tails << std::endl;
        std::cout << "Percentage of heads is  "<<  percent <<"%" << std::endl;
        std::cout << "Enter 'y' to play again, 'n' to quit: " << std::endl;
        std::cin>>repeat;
        }
        while (repeat == "y");


    }


OUTPUT:
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
Please enter the number of tosses 65
Number of heads 34
Number of tails 31
Percentage of heads is  52.3077%
Enter 'y' to play again, 'n' to quit: 
y
Please enter the number of tosses 43
Number of heads 52
Number of tails 56
Percentage of heads is  120.93%
Enter 'y' to play again, 'n' to quit: 
n
goldenchild731@ubuntu:~/Desktop$ g++ CoinToss.cpp
goldenchild731@ubuntu:~/Desktop$ ./a.out
Please enter the number of tosses 56
Number of heads 30
Number of tails 26
Percentage of heads is  53.5714%
Enter 'y' to play again, 'n' to quit: 
y
Please enter the number of tosses 45
Number of heads 19
Number of tails 26
Percentage of heads is  42.2222%
Enter 'y' to play again, 'n' to quit: 
83
Topic archived. No new replies allowed.