CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Oct 2009
    Posts
    4

    I have no idea why this isnt working...

    I compiled this code without any problem, the program runs but when I enter a value nothing happens. No error message or anything, the program essentially freezes. I'm thought it might have gotten stuck in the for loop in Flip(n) but that isnt called until the second cout statement and it doesnt even get that far...Any help would be greatly appreciated.

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

    using namespace std;

    int Flip( int in );

    int main(void)
    {

    srand( time(0) );

    int ntimes = 0;
    int YN = 0;

    do{
    cout << "how many times do you want a coin to be tossed?" << endl ;
    cin >> ntimes ;

    cout << "Out of " << ntimes << " tosses " << Flip(ntimes) << " came out heads"<< endl ;

    cout << " would you like to repeat the experiment? 1 for yes and 0 for no" << endl ;
    cin >> YN ;
    }while (YN != 0);

    cout << "ok. goodbye." << endl ;

    return 0;
    }

    int Flip( int n )
    {
    int iToss = 0;
    int iAnswer = 0;
    int iA = 0;

    for (iA = 0; iA = n; iA++)
    {
    iToss = (rand()%2);
    iAnswer = iAnswer + iToss;
    }

    return iAnswer;
    }

  2. #2
    Join Date
    Oct 2009
    Posts
    2

    Re: I have no idea why this isnt working...

    Quote Originally Posted by heinrich View Post
    for (iA = 0; iA = n; iA++)
    There is your problem. It should be iA < n. I am guessing you originally intended iA != n, but it ended up being a typo. The reason why that loop never terminates is because that assignment is evaluated to a non-zero value, which is taken to be true and hence the loop never terminates.

  3. #3
    Join Date
    Oct 2009
    Posts
    4

    Re: I have no idea why this isnt working...

    Thanks you so much! I was getting really frustrated and I didn't notice that at all... You are a life saver

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured