This program was suppose to be with a function but I am trying to write without a function first, then adding one later. I am new to programming and still gettin basics. I got this much but its only giving me a 1 and a 0. Ive tried to find the fix, can you guys help me out please?

#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

int main()
{
unsigned heads = 0;
unsigned tails = 0;
unsigned flips =  0;


cout<<"enter number of times to flip the coin."<<endl;
cin>>flips;
flips = rand() % 2;
for (int i = 0; i < flips; i ++)
{
if ( flips == 0 )
heads = heads ++;
else
tails ++;
}

cout<< " You flips tails "<<tails<<" times"<<endl;
cout<< " You flips heads "<<heads<<" times"<<endl;
system("pause");
return 0;
}

It generates only 0 and 1 because rand() % 2 only finds a number between 0 and one. To find a number between one and two you could either do flips = (rand() % 2) + 1 or flips = (rand() % 3); Also, this is unneeded heads = heads ++; . Below you do it correctly with tails++

You've tried to fix what ??? There are only two choices -- heads or tales -- 0 or 1. what else do you want?

[edit] Ohhhh, I think I see the problem. after getting user input you immediately change it to either 0 or 1. Delete that line. [/edit]

I need a user to enter the nubmer of times to flip the coin, then have it spit out how many were heads and tails. So like, I want to flip the coin 50 times. How many are heads and how many tails.

I enter say 55, and I only get output of 1 and 0.. instead of like 25 were heads and 25 were tails.. just says 1 is heads and 0 was tails.

There are two problems with your program.

1. Move line 15 down to just after line 17 -- it needs to be inside that loop.
2. don't use variable name flips at lines 15 and 18 because that destroyes the loop counter. use some other variable name.

You may want to seed your random number generator too.

#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

int main()
{
   unsigned heads = 0;
   unsigned tails = 0;
   unsigned flips =  0;

   srand(time(0));

   cout << "enter number of times to flip the coin." << endl;
   cin  >> flips;

   for ( int i = 0; i < flips; i ++ )
   {
      if ( rand() % 2 )
      {
         ++heads;
      }
      else
      {
         ++tails;
      }
   }

   cout << " You flips tails " << tails << " times" << endl;
   cout << " You flips heads " << heads << " times" << endl;
   return 0;
}

thanks for all the help, I got the program to work without the function, just trying to do that now. Thnks!

I have one last question, I am running the program with the function flip()... I need the function flip() to return the value of heads and tails back into main, without using any arguments. sorry for the bother I either really messed up or something. Functions are hard for me to understand.

#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
// using the function flip simulate a coin flipping
// user enters amount of times to flip the coin
// have your program return how many were tails 
// and how many were heads.


//Function Prototype flip
unsigned flip();
int main()
{
    
//Input number of times to flip the coin.
      cout<< "Enter the number of times you would like to flip the coin."<<endl;
//function flip 
  flip();

//Variables
           unsigned heads = 0;
           unsigned tails = 0;
           
        
       cout<< " You flips tails "<<tails<<" times"<<endl;
       cout<< " You flips heads "<<heads<<" times"<<endl;
                 
        
system("pause");
return 0;            
}


unsigned flip()
{
         
          unsigned heads = 0;
          unsigned tails = 0;
          unsigned flips;
          unsigned toss;
          
          srand(time(0));

cin>>toss;
flips = toss;

      for (int i = 0; i < toss; i ++)
{ 
  if (rand() % 2)
  { 
         heads ++;
  }
               else
  {
         tails ++;
  }
    
}

}

>I need the function flip() to return the value of heads and tails back into main, without using any arguments.
Why can't you use arguments? Functions can only return one value. Unless you put the heads and tails into some sort of aggregate type, or pack them both into a single scalar type, you simply can't do it within the confines of the language. For this sort of thing it's preferred to pass references to the function and assign to those as a sort of poor man's return.

Its for homework, he told us to use functions but no arguements... mmm ill ask him when i go to class, which i am leaving for now.

It's possible that he wants you to use global variables, but if that's the case I'd seriously consider getting a new teacher.

It's possible that he wants you to use global variables, but if that's the case I'd seriously consider getting a new teacher.

Silly things teachers make you do make/mandate in codes frustrates the heck out of everyone, it's such a turn-off for noobs like.

I just changed where you put the couts

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

using namespace std;

unsigned flip();

int main()
{
  cout << "Enter the number of times you would like to flip the coin." << endl;
  flip();

  system ( "pause" );
  return 0;
} 
unsigned flip()
{
  unsigned heads = 0;
  unsigned tails = 0;
  unsigned flips;
  unsigned toss;

  srand ( time ( 0 ) );

  cin >> toss;
  flips = toss;

  for ( int i = 0; i < toss; i ++ ) {
    if ( rand() % 2 ) {
      heads ++;
    }
    else {
      tails ++;
    }
  }
  cout << " You flips tails " << tails << " times" << endl;
  cout << " You flips heads " << heads << " times" << endl;
}

Don't forget to take a look at Salem's link if the quality of your pseudo-random number generator is an issue perhaps?

This article has been dead for over six months. Start a new discussion instead.