title: Why are my if statements not working consistently?
body: <p>I'm making a coin toss program for my c++ class and we are required to make a function that flips a coin and prints out if it is heads or tails, and print 10 per line. When I ran the program though the if statements I used to detect if the coin was heads or tails weren't enough to pick from the two.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;ctime&gt;
  using namespace std;
  
  void coinToss(int times);
  
  int main()
  {
  srand(time(0));
      int times;
      cout &lt;&lt; "How many times would you like to toss the coin?" &lt;&lt; endl;
      cin &gt;&gt; times;
  
  coinToss(times);
  
  return 0;
  }
  
  void coinToss(int times)
  {
      int toss = 0, count = 0;
      for(int i = 0; i &lt; times;i++)
      {
          toss = rand()%2;
  
          if(toss == 1)//Detects if coin is heads.
          {
              cout &lt;&lt; "H";
          }
          if(toss == 0)//Detects if coin is tails.
          {
          cout &lt;&lt; "T";
          }
  
          else //I had to include this for the program to run, further explanation below the code.
          {
          cout &lt;&lt; "Ya done goofed."; 
          }
  
          count++; //Counts to ten
          if(count == 10) //Skips to the next line if the coin has been tossed ten times.
          {
              cout &lt;&lt; endl;
              count = 0;
          }
      }
  
  }
  </code></pre>
  
  <p>At one point I replaced the heads or tails with "cout &lt;&lt; toss;" and the only numbers returned were 1 and 0. I don't understand how if I'm getting only the two numbers I'm checking for some of them aren't being caught by my if statements.</p>
  
  <p>To complete the assignment I've changed the second if statement into an else statement and everything seems peachy, but I'd really like to understand what's going on here.</p>