Hello, I'm working on a program and I'm close to finishing it, but theres one (or two) parts that I'm unsure of depending on how I set it up. The program has to have a user defined function flip(). Flip is only supposted to randomly generate a 0 or 1, and then assign H for heads, or T for tails. From there we have to call flip to main, and basically just have print out the results.

The number of flips is 100, no user input or some random number, and the output screen has to have the format of 10 rows x 10 columns of random "H" or "T"'s. I can get the flip function to work fine, however I can't format the output so it is in the 10x10 block, its driving me nuts. The other way I tried to do it was I was able to setup the 10 x 10 block, but then the random generator wouldnt work properly. This portion of the code I have here has everything setup the way it should be, but the output is either all Heads, or all Tails.

#include <iostream>
#include <iomanip>
#include <ctime>
#include <cstdlib>

using namespace std;

int heads = 0, tails = 0, cointoss;
int flip();

int main()
{
    for (int counter = 1; counter <= 10; counter++)
    {    
        for (int count = 1; count <= 10; count++)
            flip();
        cout << endl;
    }
    
    cout << endl;
    cout << "Heads =  " << heads << endl;
    cout << "Tails =  " << tails << endl;   
        
    return 0;

}


int flip()
{
    srand(time(0));
    cointoss = rand() % 2;
    if (cointoss  == 1)
    {
      cout << "H ";
      heads++;
    }
    else
    {
      cout << "T ";
      tails++;
    }        
}

the other way I was doing it (which wasnt allowed) was to have a counter in flip (int counter = 1; counter <= 100; counter++) and the heads and tails would randomly print, no problems, but the output was 100 "H" or "T"s in a single line scrolling down the screen. Every time I tried to put it in the 10 x 10 block everything would go wrong.

I'm not really looking for a "heres the solution code", rather just tips or advice on what I'm doing wrong. I can't really learn much if others do it for me, but I can't learn much if I can't get past where I'm at. Any help would be greatly, greatly apprechiated! Thanks (and sorry for the long read)

Seed the random number generator only once in your program. Move your srand call to main, before your for loops.

#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <ctime>
 
using namespace std;
int heads = 0, tails = 0, cointoss;
int flip();
int main()
{
    srand(time(0));
 
 for (int counter = 1; counter <= 10; counter++)
    {    
        for (int count = 1; count <= 10; count++)
            flip();
        cout << endl;
    }
    
    cout << endl;
    cout << "Total number of heads =  " << heads << endl;
    cout << "Total number oftails =  " << tails << endl;
 cout << "Allen Cohey ";
        
 cout << endl;
cointoss = rand() % 2;
    if (cointoss  == 1)
    {
  return 0;
      cout << "H ";
      heads++;
    }
    else
    {
  return 1;
      cout << "T ";
      tails++;
 }
    return 0;
}
int flip()
 
 
{
 
    cointoss = rand() % 2;
    if (cointoss  == 1)
    {
      cout << "0 ";
      
    }
    else
    {
      cout << "1 ";
     
    }       
return 0;
}

<< moderator edit: added [co[u][/u]de][/co[u][/u]de] tags >>

doing a same project and would like to know how to keep filp() useing 1 and 0 and still have H (heads) T (tails) come out on the screen

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.