Function Help

Started by
4 comments, last by Conner McCloud 18 years, 7 months ago
Ok once again thanks alot to the people that helped out a few days ago. All suggestions where welecome and apperciated. Now ive got a function up and running for my neon problem with transformers It has a run time problem now. When I run it the program pasues for a bit then crashes. Im going to post my code while i continue to work on it. See if any one can spot some thing wrong with it.

/*Takes in the length of neon for a sign, also takes in two adresses. Find thes
2 transformers that best match the length of neon then uses the pointers
to make there value change in the main program*/  
float transformers (float neon_length, int *transformerA_ptr, int *transformerB_ptr) 
  {
  
    /*x and y are used as counters to cycle though the transformers
    total support is test varible to see if the two transformes get
    close to adding up to the neon footage*/
    int x,y,total_support;
    
    x=0;
    y=0;
    
    
    /* This loop takes the neon footages and tests it with the first possible
    and lowest combination of transformers, then works up with the first lowest
    being added to the next highests. If that does not meet the reqiurments the
    inside loop breaks and the oustide loop makes the first transformer go up
    to the next one, and the inside loop goes again. untile a suitble match is found.*/
    

    while(1)
      {
        y=0;
        
        while(1)
          {
            total_support = 0;
            total_support = transformer[x] + transformer[y];
            
            if ( y >= 11)
              {
                break;
              }
            
            if (total_support == neon_length or total_support == neon_length+1 or total_support == neon_length+2 or total_support == neon_length+3)
              {
                break;
              }
            
            ++y;
          }
        if (total_support == neon_length or total_support == neon_length+1 or total_support == neon_length+2 or total_support == neon_length+3)
          {
            break;
          }
        
        ++x;
      }
    
    *transformerA_ptr = x;
    *transformerB_ptr = y;
    
    return(0);
  }

Again when run it pauses then crashes. Any help is welcome.
Do or do not there is no try -yoda
Advertisement
Well i am a beginner programmer, and i believe your talking about when u execute the program it quickly comes and goes away. I fix that problem with this code:

system("pause");

usually i use this right before the return 0; code. hope that solves it
No thats not the problem, this is only a header file from my main program. When this function is run, my program crashes fully.
Do or do not there is no try -yoda
The only thing I can think of is that the float neon_length never exactly equals the int total_support which causes it to go into an infinite loop and eventually access unallocated memory which crashes it.
You're probably not finding a solution, and going out of bounds on your transformer array. You never check x's upper bound, and with an infinite loop like that you're just asking to go out of bounds.

Which brings us to my next suggestion: don't use infinite loops unless you are in a situation that calls for one. You aren't. Both of those loops are better served with a for construct:
for(x=0; x < transformer_length; ++x)


CM
Quote:Original post by Anonymous Poster
The only thing I can think of is that the float neon_length never exactly equals the int total_support which causes it to go into an infinite loop and eventually access unallocated memory which crashes it.

Ahh...and there's the source of the missing solution.

Never use == with floats, always check against a range. In this case, you're clearly wanting it to be within 3 units of non_length, so changing those huge if(...) lines to the following should help a lot:
if(total_support >= neon_length && total_support <= neon_length+3)


CM

This topic is closed to new replies.

Advertisement