Hey, it looks like I'm stuck yet again. I've been working on this for awhile and I can't figure out why it's not working properly. Here's what I'm supposed to do:
Create a function that simulates coin tossing. The function should have no input and no return value. When the function is called, it first asks the number of times they want to flip the coin, say 20, then the function will call random number function rand() 20 times. Each time rand() will randomly generate two numbers between 0 and 1 and they represent the heads and tails respectively. If it's heads print H if it's tails print T.
#include<iostream>
#include<string>
using namespace std;
void displayMenu(void);
void coinToss(int flips);
int main()
{
int choice;
int flips;
string dummy;
do{
cout<<"What would you like to do?\n";
displayMenu();
cin >> choice;
switch(choice)
{
case 1:
cout <<"How many times would you like to flip the coin?"<< endl;
cin >> flips;
coinToss( flips );
getline( cin, dummy );
getline( cin, dummy );
break;
}
}
while( choice == 3 );
cout <<"The program has terminated. Good Bye"<< endl;
return 0;
}
void displayMenu( void )
{
cout << "==========================\n";
cout << "1. Flip a coin\n";
cout << "2. Multiplication\n";
cout << "3. Quit\n";
cout << "==========================\n";
}
void coinToss( int flips )
{
int counter = 0, head = 0, tail = 0, toss;
while( flips != 0 )
{
toss--;
counter++;
toss = rand() % 1;
if( toss == 1 )
{
head++;
cout << "H" << endl;
}
else
{
tail++;
cout << "T" << endl;
}
}
}
The problem that I'm having is that when I run the program it always gives me all Tails and one head. So if you have any ideas as to why it's doing that I'd appreciate the help. Thanks again for all the help.