Program freezing while running a function, please help

This is a discussion on Program freezing while running a function, please help within the C Programming forums, part of the General Programming Boards category; I'm not getting a compile error or warning, but the program seems to freeze while running the following function. As ...

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    5

    Program freezing while running a function, please help

    I'm not getting a compile error or warning, but the program seems to freeze while running the following function. As input, I send limcounter,
    which is the number of actual values in the array (the user inputs values, and can stop input before filling the array, in which case the rest of the elements in the array are 0), and the array with the inputed values.

    Does anyone know why it wouldn't be working right, I've stared at this for awhile and I'm stumped


    Code:
    int factorFind (int limcounter, int ary2 [])
    {
      //Declarations
      int indic;     //indicator that factor has been found
      int counter2;     //counter
      int counter3;     //counter
      int factor;     //calculated factor
      int noindic;     //indicator that there is no factor
    
      //Statements
      indic = 0;
      noindic = 0;
      factor = 1;
      while ( indic == 0 )
      counter3 = 0;
      {
        for ( counter2 = 0; counter2 <= ( limcounter - 1 ); counter2++ )
        {
          if ( factor % ary2[counter2] == 0 )
          {
            ++counter3;
          }
        }
        if ( counter3 == ( limcounter - 1 ) )
        {
          indic = 1;
        }
        else if ( counter3 == limcounter )
        {
          indic = 1;
          noindic = 1;
        }
        else
        {
          ++factor;
        }
      }
    
      if ( noindic == 1 )
      {
        factor = 0;
      }
    
      return (factor);
    }  //factorFind

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Code:
    while ( indic == 0 )
      counter3 = 0;
    Once you get in that loop, there's no way out of it.

  3. #3
    Registered User
    Join Date
    Jul 2011
    Posts
    5
    Quote Originally Posted by tabstop View Post
    Code:
    while ( indic == 0 )
      counter3 = 0;
    Once you get in that loop, there's no way out of it.
    I don't understand, wouldn't
    Code:
    if ( counter3 == ( limcounter - 1 ) )
        {
          indic = 1;
        }
        else if ( counter3 == limcounter )
        {
          indic = 1;
          noindic = 1;
        }
    cause the program to exit the while loop?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    But you have to get out of the loop before you can get to that part, since it is after the while loop. (C goes from top to bottom.)

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You've misplaced your braces....

    Code:
      while ( indic == 0 )  // this is the entire loop
      counter3 = 0;
    
    // this is not part of the loop
    
      {
        for ( counter2 = 0; counter2 <= ( limcounter - 1 ); counter2++ )
        {
          if ( factor % ary2[counter2] == 0 )
          {
            ++counter3;
          }
        }
        if ( counter3 == ( limcounter - 1 ) )
        {
          indic = 1;
        }
        else if ( counter3 == limcounter )
        {
          indic = 1;
          noindic = 1;
        }
        else
        {
          ++factor;
        }
      }
    
      if ( noindic == 1 )
      {
        factor = 0;
      }

  6. #6
    Registered User
    Join Date
    Jul 2011
    Posts
    5
    ..I feel really dumb about that error, but thank you so much for your help

  7. #7
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    If you learn from your mistakes, you're not dumb.
    Code:
    while(!asleep) {
       sheep++;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. program freezing
    By Beowolf in forum C Programming
    Replies: 2
    Last Post: 09-10-2007, 06:38 PM
  2. Invoke a function from another running program (EXE) in C/C++
    By sureshobabu in forum C++ Programming
    Replies: 3
    Last Post: 12-19-2006, 06:32 AM
  3. Why is my program freezing?
    By ShadowMetis in forum Windows Programming
    Replies: 8
    Last Post: 08-20-2004, 04:20 PM
  4. Program freezing..
    By xlnk in forum Networking/Device Communication
    Replies: 2
    Last Post: 11-17-2003, 09:52 PM
  5. function problem - freezing
    By Vber in forum C Programming
    Replies: 4
    Last Post: 03-21-2003, 06:40 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21