need some help please

Ok guys here is the assignment...Write a program that simulates a coin flip. Run it many times—do the results look random to you? I managed to write the program but now i need to make it so that the amount of time the coin is flip is based of the user input, so if the user input 10 it will loop ten times or if the user input 5 it will loop five times. I know that there need to be another integer to store the user input but i have no idea on how to make it so the loop works with user input any help would be truly greatfu thanks in advance. I include a copy of the code i have....

#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>

using namespace std;

int tailsCounter = 0;
int headsCounter = 0;

void coinFlip() // function for coin flip
{
int coin;
coin = rand() % 2;
if (coin == 1)
{
headsCounter++;
cout << "heads\n" << endl;

}
else
{
tailsCounter++;
cout << "tails\n" << endl;
}
}//end of function for coinFlip

int main ()
{
srand (time(0) );
for ( int x = 0; x < 100; x++ )
{
coinFlip(); // calling function
}//end of for loop

cout << "Heads was flip " << headsCounter << " Times\n" << endl;
cout << "Tails was flip " << tailsCounter << " Times\n" << endl;
}//end of main
Here is how to get user input and store it in an int.

1
2
int userInputValue; // create the int
cin >> userInputValue; // fetch value from keyboard 


Is that enough?
Hi chaosGlaaze - as Moschops said you need to use the cin >> operator - this will read the users input value into what ever variable you set i.e.
1
2
int coinFlips = 0;  //Initialise input variable to 0
cin >> coinFlips;  //Read users input value into the variable when running the program 


Now I'm assuming that this program needs to have an option to re-run it at the end of each series of coin flips by the user entering a 'yes' or 'no' condition.

To do this you need to contain your code in a while loop which will keep re running the code so long as the user presses 'yes' if they enter 'no' the program will end.

To do this, you should define another int variable of repeat and set it to 1 in the loop.
I rewrote a bunch of your code - feel free to remove what you want, I also commented out in uppercase the areas I added, I hope you can understand and reference them for your own benefit! :)

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
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include "assert.h"//ALLOWS US TO TERMINATE THE PROGRAM AT THE END ONCE CONDITIONS ARE TRUE
#include "windows.h"//ALLOWS US TO CALL THE SLEEP FUNCTION TO CREATE COUNTDOWN CLOCK AT END

using namespace std;

					
		int tailsCounter = 0;
		int headsCounter = 0;
		int flipTimes;//USER CAN USE THIS VAR TO READ AN INPUT INTO THE VARIABLE USING cin >> OPERATOR
		int rerun = 1; //UPON USER ENTERING A VALUE OF '1' THE PROGRAM WILL RETUN
		
	
		void coinFlip(int flipTimes)// function for coin flip PASSED int flipTimes TO GIVE coinFlip A CONDITION - WE CAN PASS x INTO THE LATER
		{
		
		int coin;
			coin = rand() % 2;

		if (coin == 1)
		{
			headsCounter++;
			cout << "heads\n" << endl;

		}
		else
		{
		tailsCounter++;
		cout << "tails\n" << endl;
		}
		}//end of function for coinFlip

int main ()
{
		
	while(rerun == 1){ //WHILE LOOP TO ALLOW CODE TO RE-RUN
		
		int x; //DEFINE int x WHICH IS OUR FLIP INPUT

		srand (time(0) );
		cout << "How many times would you like to flip the coin?" << endl; //OUTPUT TO THE USER PROMPTING FOR FLIP TIMES
		cin >> x;	//USER INPUTS THEIR PREFERRED VALUE OF FLIPS
			for ( int i = 0; i < x; i++ )//CHANGED THE INCREMENTER TO i SO THAT I CAN PASS VALUE x TO coinFlip();
			{
			coinFlip(x); // calling function
			}//end of for loop

		cout << "Heads was flip " << headsCounter << " Times\n" << endl;
		cout << "Tails was flip " << tailsCounter << " Times\n" << endl;
		cout << "The coin was flipped a total of: " << headsCounter + tailsCounter << " times." << endl << endl << endl; //OUTPUT THE TOTAL TIMES FLIPPED
		cout << "Would you like to play again, please enter 1 for 'yes' or 0 for 'no'?" << endl; //PROMPT USER TO PLAY AGAIN
		cin >> rerun; //READ IN USER INPUT TO RERUN OR END PROGRAM
		headsCounter = 0;//RESETS VALUE OF HEADS BACK TO 0 UPON NEW GAME
		tailsCounter = 0;//RESETS VALUE OF TAILS BACK TO 0 UPON NEW GAME

	}	//END OF WHILE LOOP
		cout << "Program shall terminate in: " ;
	
		for(int i = 3; i > 0; i --){ //A 3 SECOND COUNTDOWN TIMER IS CREATED - WHILE INT i IS GREATER THAN 3, DECREMENT i BY 1
		cout << i << " ";  //OUTPUT i's VALUE TO THE USER
				Sleep(500);   //PAUSE FOR 3 HALF SECONDS
	 
		assert(rerun == 0);//ASSERT TERMINATES THE PROGRAM WHEN ITS CONDITIONS ARE MET

		}

}//end of main  


I tested this on my computer and it works fine - if you have any further problems please let me know!


Alex.
Hi Alex.
I would change the Sleep function to something else like.
1
2
3
4
5
6
7
8
9
10
11
12
13
void pause(unsigned int milliseconds)
{
	time_t final= milliseconds + clock();
	while(milliseconds < final)
	{
		milliseconds= clock();
	}
}
......
......
......
pause(1500); // delay for approx 1.5 seconds


I'll save that into my code snippets!

I'm relatively new to C++, would you mind explaining the benefits of using a function like this opposed to the one I created? )
Hello alex first off thanks for your help I read over your code and compare it to mine and saw what i was doing wrong. However i'm very new to programing and don't understand or know what the header file assert.h, and windows.h do so i didn't use them in the code. I edit my code with the example you provide and change it a bit, it compiled and works fine how ever now i'm having a problem with terminating the program properly. when i press 1 it runs again as it should but if i press any number other than 1 it terminate. I only want it to terminate with the specific button that i ask the user to input and if they input the wrong button then it should say they have input a incorrect number. I'm thinking I may need to put in a if statement or a case statement in there but don't know how to go about it.... I reposted it if you don't mind taking a look at it thanks in advance.

#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>

using namespace std;

int tailsCounter = 0;
int headsCounter = 0;
int coinToss = 0;
int rerun = 1;

void coinFlip()
{
int coin;
coin = rand() % 2;
if (coin == 1)
{
headsCounter++;
cout << "heads\n" << endl;

}
else
{
tailsCounter++;
cout << "tails\n" << endl;
}
}//end of coinFlip

int main ()
{
while ( rerun == 1)
{
srand (time(0) );
cout << "How many times would you like to flip the coin" << endl;
cin >> coinToss;

for ( int x = 0; x < coinToss; x++ )
{
coinFlip();
}//end of for loop


cout << "Heads was flip " << headsCounter << " Times\n" << endl;
cout << "Tails was flip " << tailsCounter << " Times\n" << endl;
cout << "Do you want to flip the coin again ? press 1 for yes and 2 to quit\n " << endl;
cin >> rerun;
headsCounter = 0;
tailsCounter = 0;
}//end of while loop
}//end of main
Last edited on
Hey again - for future reference the 'assert.h' file allows us to use the assert function in our program - its basically a short function which takes a conditional parameter in which if the condition is true the console will close, I only used that in your program to terminate the program but this can be done in a variety of different ways.
'windows.h' allows for a variety of other predefined functions to be called into your program, but in my first example I provided I used it to access the Sleep(); function in order to create the count down clock - these are obviously not required!
As for your problem with the error message you are correct in saying we could use an if statement - I shall include the full amended code below!

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

using namespace std;

int tailsCounter = 0;
int headsCounter = 0;
int coinToss = 0;
int rerun = 1;

void coinFlip()
{
int coin;
coin = rand() % 2;
if (coin == 1)
{
headsCounter++;
cout << "heads\n" << endl;

}
else
{
tailsCounter++;
cout << "tails\n" << endl;
}
}//end of coinFlip

int main ()
{
while ( rerun == 1)
{
srand (time(0) );
cout << "How many times would you like to flip the coin" << endl;
cin >> coinToss;

for ( int x = 0; x < coinToss; x++ )
{
coinFlip();
}//end of for loop


cout << "Heads was flip " << headsCounter << " Times\n" << endl;
cout << "Tails was flip " << tailsCounter << " Times\n" << endl;
cout << "Do you want to flip the coin again ? press 1 for yes and 2 to quit\n " << endl;
cin >> rerun;
headsCounter = 0;
tailsCounter = 0;

//HERE IS THE IF STATEMENT TO ONLY ALLOW INPUTS OF 1 AND 2 
if(rerun < 1 || rerun > 2) {
	cout << "Error you have not entered a valid value - Enter 1 to play again or 2 to quit" << endl;
	cin >> rerun;
}//end of if statement

}//end of while loop

}//end of main 


I hope this solves your problem - you can find the if statement at the very bottom of the code, if you've any other problems please ask!

Regards

Alex.
Thank you alex you have been a great help. I have not gotten that far with the assert.h and window.h as of yet.
No problem! If you get stuck on anything in future feel free to private message me

I'm sure you'll stumble accross them sooner or later, good luck!
just try to PM you another problem but it says you don't accept PM i posted it on the forum if you have time your help would be appreciated
Topic archived. No new replies allowed.