I was trying to create a program that simulates tossing a coin to a user specified number of times and then say how many times heads appeared. However when i run my program it only seems to be looping once.

//generates a random number 1-10
int rdom_gen()
{
    srand((unsigned)time(0));
    int rdom_int;
    rdom_int = (rand()%10) + 1;
    return rdom_int;
}

//Takes a random number 1-10 and returns a 1 if less or equal 5
int onetesthead()
{
    srand((unsigned)time(0));
    int testing = rdom_gen();
    if (testing <= 5 )
    return 1;
    else 
    return 0;
}

//This is supposed to be the loop that simmulates cointosses
int coinflip(int numtimes)
{
     for (int i = 0; i < numtimes; i ++ )
     { 
     int numofheads ;
     numofheads = numofheads + onetesthead();

     cout << "test loop";
     cout << endl;
     
     return numofheads;
     }
}
int main()
{
    int index;
    cin >> index;
  
    int numofheads = coinflip(index);
    cout << numofheads;
    cout << endl;
   
}
Comments
Used code tags correctly on first post.
//This is supposed to be the loop that simmulates cointosses
int coinflip(int numtimes)
{
     for (int i = 0; i < numtimes; i ++ )
     { 
     int numofheads ;
     numofheads = numofheads + onetesthead();

     cout << "test loop";
     cout << endl;
     
     return numofheads;
     }
}

The line in red is going to short-circuit your loop because it is INSIDE your for-loop. Perhaps you want this line AFTER the for-loop is complete?

//This is supposed to be the loop that simmulates cointosses
int coinflip(int numtimes)
{
     for (int i = 0; i < numtimes; i ++ )
     { 
     int numofheads ;
     numofheads = numofheads + onetesthead();

     cout << "test loop";
     cout << endl;
     
     return numofheads;
     }
}

The line in red is going to short-circuit your loop because it is INSIDE your for-loop. Perhaps you want this line AFTER the for-loop is complete?

when i put return numofheads outside of the loop, then it says numofheads is undeclared.

Yes, if you take the return statement out of the loop, you must also take numofheads out of the loop so it stays "in scope". Put it BEFORE the loop starts and put the return statement AFTER the loop. Also, don't forget to initialize it! Don't assume the compiler will do it for you, even though some will.

//This is supposed to be the loop that simmulates cointosses
int coinflip(int numtimes)
{
     int numofheads = 0;
     for (int i = 0; i < numtimes; i ++ )
     { 
     numofheads = numofheads + onetesthead();

     cout << "test loop";
     cout << endl;    

     }
     return numofheads;
}

oh thanks yeah i figured out what you said after your first reply, so now it loops but then numofheads always equals the index value or 0. My guess is there is something going wrong with the random number generator part.

You just want one call to srand((unsigned)time(0)); . Put it as the first statement in main and remove it from everywhere else (it's in two functions now).

oh thanks yeah i figured out what you said after your first reply, so now it loops but then numofheads always equals the index value or 0. My guess is there is something going wrong with the random number generator part.

//generates a random number 1-10
int rdom_gen()
{
    srand((unsigned)time(0)); // put this line at top of main
    int rdom_int;
    rdom_int = (rand()%10) + 1;
    return rdom_int;
}

You are going to get really lousy random numbers (i.e. potentially all the same) if you keep reseeding the RNG. Seed it ONCE and only once at the beginning of the program. Take it out of the function and put it at the top of main.

oh my gosh thanks! that worked. Thank you both of you guys for helping me, its only my 4th day working with c++.

sup dawg i herd you like to loop while you loop so i put a coin in

... oh, never mind. that's so last September.

LOL....
anyways thanks guys got my program up and running and it also computes the confidence interval!

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