Jump to content
  • Advertisement
Sign in to follow this  
Xloner12

Function Help

This topic is 4813 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
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

Share this post


Link to post
Share on other sites
No thats not the problem, this is only a header file from my main program. When this function is run, my program crashes fully.

Share this post


Link to post
Share on other sites
Guest 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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!