HELP :-(

Write a program that simulates coin tossing. For each toss of the coin, the
program should print Heads or Tails. Let the program toss the coin 100 times and count the number
of times each side of the coin appears. Print the results. The program should call a separate function
flip that takes no arguments and returns 0 for tails and 1 for heads. [Note: If the program
realistically simulates the coin tossing, then each side of the coin should appear approximately half
the time.]

I'M NOT BEING ABLE TO RUN THIS CODE EVERY TIME I TRY TO DEBUG IT THE BLACK WINDOW CLOSES RIGHT AFTER , AND I DON'T HOW TO MAKE IT WORK . HELP PLEASE.


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
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
 
#include <cstdlib>
 
#include <ctime>
 
int flip(void);
 
int main()
{
   srand( time(0) );
   int t = 0;
   int h = 0;
 
   for( int i = 1; i <= 100; i++)
   {
      if(flip() == 0)
      {
         cout << "Tails" << endl;
         t++;
      }
      else
      {
         cout << "Heads" << endl;
         h++;
      }
 
   }
   cout << "Tails count: " << t << "\nHeads count: " << h << endl;
   return 0;
}
 
int flip(void)
{
   return rand() % 2;
}






See if this article helps:
http://www.cplusplus.com/articles/iw6AC542/
Topic archived. No new replies allowed.