coin toss then repeat with a different input

Write your question here.
I have completely the program for coin toss, however I want to do it again with a different USER INPUT.

1) it works, but doesn't allow to put a new user input after the first one runs

2) i was hoping to add the 1st run and 2nd run then calculate the average of both run at the end.... (p.s) i have the percentage of the 1 run, but after that it adds up and make it looks like over 100+%)

3) i was using the for loop and then if-else statement

here's my code... I must have done ONE TINY thing wrong and i can't quite put my finger on it....

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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>

using namespace std;


int coinFlip(void)
{
    int randNum;
    randNum = rand()%2; //you will get the value 0 or 1
    return randNum;
}

int main()

{
    int NumTimes;
    int randNum, heads=0, tails=0;
    int answ;
    int y;
    
    
    float headPercentage, tailPercentage; //dealing with decimal value
    
    
    cout<< "How many times are we going to do coin toss?";
    cout<< " MUST BE POSITIVE NUMBER!!" << endl;
    cin>> NumTimes;         //user input
    
    //To make the game more interesting, get a different series of random numbers each time by using the time that the program is run as the seed value for the random number generator using "srand((time(0)))"
    
    srand(static_cast<unsigned int>(time(0))); // random number of seeding for Xcode

    for(int i=1; i<=NumTimes; i++)          // starts; counts to; n+1 each time
    {
        randNum = coinFlip ();
        
        if(randNum == 1)                   // if its equal to 1
        {
            heads++;
        }
        else
        {
            tails++;
        }
        
    }
    
    // The result after the number of coin toss has been made
    
    cout<<"The no. of heads are " <<heads << endl;
    cout<<"The no. of tails are " <<tails << endl;
    
    // need to do percentage of heads and tails
    //float is required to make the value into a decimal before *100
    
    headPercentage = (float)heads/NumTimes*100;
    tailPercentage = (float)tails/NumTimes*100;
    
    //"setprecision()" is required to set the amount of digits after the decimal
    
    cout<< "The percentage of heads is " << fixed << setprecision(1) <<headPercentage << "%" <<endl;
    cout<< "The percentage of tails is " << tailPercentage << "%" <<endl;
    
{
        cout<< "Want to run it again? (Y/N) " <<endl;
        cin>> answ;
        
if(y==y)
    {
        cout<< "How many times are we going to do coin toss?";
        cout<< " MUST BE POSITIVE NUMBER!!" << endl;
        cin>> NumTimes;         //user input
        
        //To make the game more interesting, get a different series of random numbers each time by using the time that the program is run as the seed value for the random number generator using "srand((time(0)))"
        
        srand(static_cast<unsigned int>(time(0))); // random number of seeding for Xcode
        
        for(int i=1; i<=NumTimes; i++) // starts; counts to; n+1 each time
        {
            randNum = coinFlip ();
            
            if(randNum == 1)                   // if its equal to 1
            {
                heads++;
            }
            else
            {
                tails++;
            }
            
        }
        
        // The result after the number of coin toss has been made
        
        cout<<"The no. of heads are " <<heads << endl;
        cout<<"The no. of tails are " <<tails << endl;
        
        // need to do percentage of heads and tails
        //float is required to make the value into a decimal before *100
        
        headPercentage = (float)heads/NumTimes*100;
        tailPercentage = (float)tails/NumTimes*100;
        
        //"setprecision()" is required to set the amount of digits after the decimal
        
        cout<< "The percentage of heads is " << fixed << setprecision(1) <<headPercentage << "%" <<endl;
        cout<< "The percentage of tails is " << tailPercentage << "%" <<endl;
        {
            
            cout<< "Want to run it again? (Yes or No) " <<endl;
            cin>> answ;
    }
    }
else
    {
    cout<< "You have decided to Quit ";
    }
return 0;

}
}
  Put the code you need help with here.
You need a loop - http://www.tutorialspoint.com/cplusplus/cpp_loop_types.htm

use std::string when you ask the user if they want to play again. int is for integers, std::stringis for characters/words like yes and no. You have to #include <string> too.

The String Class in C++:
http://www.tutorialspoint.com/cplusplus/cpp_strings.htm

1
2
3
4
5
6
7
8
do
{
    //all the code
    std::string runAgain;
    cout<< "Want to run it again? (Yes or No) " <<endl;
    cin>> runAgain;
    
}while(runAgain == "Yes");
Last edited on
so then i need to use std::string for NumTimes, y, n?
Well, NumTimes is the amount of times the user wants to throw the coin, that's a number, isnt it? Why would you want to use std::string?
i want the user to ru again but this time the user pick a different number for NumTimes

example:
user put NumTimes=78 (round 1)

user put NumTimes= 98 (round 2)

user put NumTimes= 34 (round 3)

etc

etc
Last edited on
Yes. Use a loop.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>

int main()
{
	std::string runAgain;
	int NumTimes = 0;
	do
	{
		//Ask user
		std::cout << "How many times are we going to do coin toss?";
		std::cout << " MUST BE POSITIVE NUMBER!!" << std::endl;
		std::cin >> NumTimes;         //user input

		//Rest of code
		
		//Ask user if they want to repeat
		std::cout << "Want to run it again? (Yes or No) " << std::endl;
		std::cin >> runAgain;

		//If answer is "Yes" Then it will run again, starting from the top and it will
		//ask the user for a different number for NumTimes.
	} while (runAgain == "Yes");
}

Last edited on
got it thanks.... i need to review the do loop and the purpose of std:: because obviously i must have miss those informations

thanks
@TarikNeaj

Everything works perfectly, however it adds the round 1 user input and add round 2 user input then head percentage and tail percentage was another issue of its own because it since it doesn't properly calculate the percentage of round 2 alone of course...
Last edited on
I can't help you with your problem unless you post your newely updated code =)
Topic archived. No new replies allowed.