Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Problem with break, maybe?

  1. Jan 15, 2009 #1
    Hi again,

    I *think* im having a problem with break, as I cant figure out why I am having the problem Im 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 (Text):
    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: Jan 15, 2009
  2. jcsd
  3. Jan 18, 2009 #2
    Why do you have a semicolon at the end of the for-loop line? That doesn't seem right.

    Code (Text):
    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 (Text):
    for (i = 0; (i < traitCount) && (success != 0); i++)
    {
       testMin = ...
     
    Last edited: Jan 18, 2009
  4. Jan 18, 2009 #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: Jan 18, 2009
  5. Jan 18, 2009 #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: Jan 18, 2009
  6. Jan 18, 2009 #5
    Nah, the problem isnt 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. Ive 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 ive posted...
     
    Last edited: Jan 18, 2009
  7. Jan 21, 2009 #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 im doing that wrong? Anyway, here is the code.

    Code (Text):
    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.
     
  8. Jan 25, 2009 #7
    Can't you debug it with a debugger to see exactly what it is doing?
     
  9. Jan 25, 2009 #8

    DaveC426913

    User Avatar
    Gold Member

    Where is traitcount initialized? And to what?
     
  10. Jan 26, 2009 #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.
     
  11. Jan 26, 2009 #10
    Good!

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

    i = traitCount;
     
  12. Jan 26, 2009 #11
    Whoops, I said something stupid. Yeah, that looks as if it should work
     
  13. Jan 26, 2009 #12

    DaveC426913

    User Avatar
    Gold Member

    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.
     
  14. Jan 26, 2009 #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.
     
  15. Jan 27, 2009 #14
    If you wanna really avoid break, then change the for loop into while loop with the success condition. Or if you wanna 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: Jan 27, 2009
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: Problem with break, maybe?
  1. Problems with zheev (Replies: 4)

  2. Javascript problem (Replies: 6)

  3. Problem with dgesv (Replies: 5)

Loading...