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....
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
usingnamespace 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<unsignedint>(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<unsignedint>(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.
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.
#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");
}
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...