I'm working on a coin flipping program that uses boolians to determine what message to display to the user, but I've run into a little problem... It only displays that the coin flips heads. Here is the Source code.
#include <iostream>
#include <ctime>
#include <string>
using namespace std;
void main ()
{
srand (time(NULL));
int HowManyFlips = 0;
int TotalHeads = 0;
int TotalTails = 0;
bool CoinResult;
string UserChoice;
//Find out how many times to flip the coin
cout <<"How many times do you want to flip the coin?"<< endl;
cin >> HowManyFlips;
cout << endl;
//Flip the coin as many times as the user suggests
while (HowManyFlips != 0)
{
//Find the Coin side (I'm thinking this is where the problem is but I can't figure out why)
CoinResult = rand()% 2;
//Display Result of coin to user and add to total heads/tails
if (CoinResult = true)
{
cout <<"The coin landed on Heads"<< endl;
TotalHeads++;
}
else
{
cout <<"The coin landed on Tails"<< endl;
TotalTails++;
}
//reduce Flips so that the process isn't infinite
HowManyFlips--;
}
cout << endl;
//display how many heads/tails were flipped
cout <<"There are "<<TotalHeads<<" Total Heads"<< endl;
cout <<"There are "<<TotalTails<<" Total Tails"<< endl;
cout << endl;
//find out if user want's to flip again
cout <<"Do you want to flip again?(y/n): ";
cin >> UserChoice;
if (UserChoice == "y")
{
cout << endl;
main();
}
else
{
return;
}
}
If I could get some help please I would be grateful.
Add your 2¢