Help with a function please!

Ok my professor wanted me to wrtie a function that would simulate a coin being tossed and to display what the result of each flip was. I've done that part correctly, but we also have to display number of times it was heads and tails each and thats the part I cannot figure out how to do. Any advice?

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
#include <iostream>
#include <iomanip>
using namespace std;

void coinToss(int);



int main()
{
int A,B,seed,toss;
seed=time(0);
srand(seed);

cout << "How many times would you like to toss the coin? ";
cin >> toss;

for(int loop=0;loop<toss;loop++)
{
coinToss(A);
}


return 0;
}

void coinToss(int A)
{
A=rand()%2+1;

	if (A==1)
	{
		cout << "Heads" << endl;
	}
	else if (A==2)
	{
		cout << "Tails" << endl;
	}
	
}

Why does your function take a parameter? Why not:
27
28
29
void coinToss()
{
	int A = rand()%2+1;


You should consider returning a value from your function and tracking the number of heads and tails in main. (Ideally, you should be printing "Heads" and "Tails" in main too, but I guess you have to follow your professor's weird instructions)
I guess its just the way I saw it in my notes and its been working with any function I've ever had to use so I thought it was just done that way.
The point of a function taking parameters is that the parameters affect the behavior of the function. The way you had your code originally, you were completely ignoring the parameter. Not only that, the value you were passing to the parameter in the first place was never assigned a value and so just had some garbage value.
Topic archived. No new replies allowed.