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

  • Thread starter VirObitus
  • Start date
  • Tags
    Break
In summary, the code is supposed 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. However, the code is having a problem, as it is always selecting the first choice.
  • #1
VirObitus
19
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
  • #2
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:
  • #3
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:
  • #4
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:
  • #5
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:
  • #6
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.
 
  • #7
Can't you debug it with a debugger to see exactly what it is doing?
 
  • #8
Where is traitcount initialized? And to what?
 
  • #9
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:

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

1. What is a "Problem with break, maybe?"

A "Problem with break, maybe?" refers to an issue that occurs when there is a malfunction or failure in a physical or mechanical object, such as a machine or device.

2. How do I know if I have a "Problem with break, maybe?"

You may have a "Problem with break, maybe?" if you notice any unusual noises, vibrations, or changes in performance of the object. It is important to address these issues as soon as possible to prevent further damage.

3. What causes a "Problem with break, maybe?"

A "Problem with break, maybe?" can be caused by a variety of factors, including wear and tear, poor maintenance, or a manufacturing defect. It is important to identify the root cause in order to properly fix the problem.

4. How can I fix a "Problem with break, maybe?"

The best way to fix a "Problem with break, maybe?" is to consult the user manual or seek professional help. Depending on the severity of the issue, it may require replacing parts, adjusting settings, or performing routine maintenance.

5. How can I prevent a "Problem with break, maybe?" from happening?

To prevent a "Problem with break, maybe?" from occurring, it is important to regularly maintain and service the object according to the manufacturer's recommendations. This can help identify any potential issues before they become bigger problems.

Similar threads

  • Programming and Computer Science
Replies
4
Views
774
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
20
Views
1K
  • Programming and Computer Science
Replies
1
Views
963
  • Programming and Computer Science
Replies
19
Views
2K
  • Programming and Computer Science
Replies
1
Views
681
Replies
63
Views
3K
  • Programming and Computer Science
Replies
17
Views
1K
Back
Top