How can I fix the problem I'm having with break in my code?

  • Thread starter Thread starter VirObitus
  • Start date Start date
  • Tags Tags
    Break
AI Thread Summary
The discussion revolves around troubleshooting a code issue related to using a break statement within a loop that checks if 24 integer traits fit within specified minimum and maximum values. The original code fails to exit the loop correctly, primarily due to a misplaced semicolon after the for-loop declaration, which caused unintended behavior. Participants suggest combining the success condition with the for-loop's termination condition to eliminate the need for break. While the code eventually works, the user seeks alternatives to break, expressing concerns about coding style. Suggestions include using a while loop that checks the success condition instead of relying on break to exit the loop.
VirObitus
Messages
19
Reaction score
0
Hi again,

I *think* I am having a problem with break, as I can't figure out why I am having the problem I am having. This code is meant to compare 24 integer 'traits' against 48 minimum and maximum values, and if all 24 fit within their respective min and max value, to exit the loop. If the response (saythis) fails even one of the min/max tests, it should pick a new saythis. Right now it does the same thing every time - throws out exactly 1 saythis, and chooses the second one.

I've also been told to avoid using break, as it is bad coding style (or something), so if there is another way to accomplish this, I would love to see it.

Code:
int success = -1;
int traitCount = 24;
int ResponseCount = 120;
int TraitMatrix[120][24]; // if this array were graphed, all the odd numbers would be min, and all the even ones would be max
int a = 0;
int b = 1;

     while (success < 1)
     {              
         saythis = rand()%responseCount;     //creates a random number between 0 and responseCount and assigns it to saythis

         printf("first saythis is: %d\n\n", saythis);  //debug - see each random number as it is selected
                     
         for (i = 0; i < traitCount; i++);
         {
             if (success == 0)
             {
                break;
             }
             testMin = traitMatrix[saythis][a];  //sets the minimum possible value of the trait
             testMax = traitMatrix[saythis][b]; //sets the maximum possible value of the trait
                    
             if (traits[i] >= testMin && traits[i] <= testMax) //tests the NPC's actual trait against the min and max value
             {
                a = a+2;
                b = b+2;
                success = 1;
             } 
             else
             {
                 success = 0;
             }
         }
     }
 
Last edited:
Technology news on Phys.org
Why do you have a semicolon at the end of the for-loop line? That doesn't seem right.

Code:
for (i = 0; i < traitCount; i++);
{
   ...
}

That semicolon terminates the for-block; the following block is executed only once, after the empty for-loop terminates.

If you fix that, you can get rid of the 'break' by combing your break test with the conditional in the for loop:

Code:
for (i = 0; (i < traitCount) && (success != 0); i++)
{
   testMin = ...
 
Last edited:
Aha, thank you for pointing that out... stupid mistake. The success test in the for loop is a great idea, I didn't even think of that, thanks.

However, the problem still remains... except now it just always selects the first choice.
 
Last edited:
What's the full source file? You've declared all of the variables you are using, right? Your arrays have meaningful numbers stored in them, right?

There's a couple more probable errors: you misspell several variables. You've declared 'ResponseCount' but use 'responseCount', and ditto with 'TraitMatrix'. Hope this helps.
 
Last edited:
Nah, the problem isn't the variables, that would cause a compile error (that was just me not paying attention when i posted them) The full source is about 500 lines, so I would rather not post the entire thing. I've troubleshot(sp?) the rest of the program, and it compiles and run fine, and the arrays are declared and assigned correctly, the problem is with testing the min/max array against the traits, so it has to be in this bit of code I've posted...
 
Last edited:
Ok, I had a hell of a time troubleshooting this thing, and if anyone is interested, this is how I finally got it to work. However, it still uses break, which I am told is 'bad programming', so if anyone can show me a way to avoid it I would be grateful. I tried " for (i = 0; i < traitCount || success != 0; i++) " but it causes an infinite loop. Maybe I am doing that wrong? Anyway, here is the code.

Code:
success = 2;

     while (success != 1)
     {
         a = 0;
         b = 1;
         saythis = rand()%responseCount;                                           //creates a random number between 0 and loopCount2 and assigns it saythis
         printf("first saythis is: %d\n\n", saythis);                         //debug***  see each random number as its selected -
                     
         for (i = 0; i < traitCount; i++)
         {
             
             //printf("success is: %d\n\n", success);
             testMin = traitMatrix[saythis][a];                                    //sets the minimum possible value of the trait
             testMax = traitMatrix[saythis][b];                                    //sets the maximum possible value of the trait
                                                 
             //printf("testMin is: %d\n", testMin);
            //printf("testMax is: %d\n", testMax);
             
             if (traits[i] >= testMin && traits[i] <= testMax)             //tests the NPC's actual trait against the min and max value
             {
                a = a+2;
                b = b+2;
                success = 1;
             }
             else
             {
                 success = 0;
             }
             
             if (success == 0)
             {
                 break;
             }
         }
     }

Edit: As a side note, the reason my OP code was not working (besides the semicolon mistake) was that success == 0 was tested first, thus causing an infinite loop if success ever did == 0. Live and learn.
 
Can't you debug it with a debugger to see exactly what it is doing?
 
Where is traitcount initialized? And to what?
 
As I said above, the last code I posted is working correctly, no need to debug it (unless your talking about the for loop?). Just curious if there is a way to do that without using break. traitCount is set by reading a file, and is set to the number of lines in the file.
 
  • #10
Good!

In place of your 'break', you could write..

i = traitCount;
 
  • #11
Whoops, I said something stupid. Yeah, that looks as if it should work
 
  • #12
VirObitus said:
As I said above, the last code I posted is working correctly, no need to debug it (unless your talking about the for loop?). Just curious if there is a way to do that without using break. traitCount is set by reading a file, and is set to the number of lines in the file.

So:
- you're reading from a file
- you're using a loop to perform some function(s) on each line
- you don't know how many lines there are in the files, at least until you reach the end
- once you reach the end you want to break out of the loop

This is a textbook case for a while (!myfile.eof){} loop.
 
  • #13
Not really Dave. I've already finished reading the file by the time this function is called, its just testing the results of the file that it read against another array. traitCount just holds the number of entries in the file, so that the program knows when to stop. It doesn't manipulate the file at all, the file just tells it values to check against other values.
 
  • #14
If you want to really avoid break, then change the for loop into while loop with the success condition. Or if you want to really piss off purists, you can set i to traitcount right after success=1. that would teach them a lesson.ah someone else suggested that already.
 
Last edited:
Back
Top