Thread: I got a bug in my program

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    308

    I got a bug in my program

    Here is the link to my github:
    https://github.com/jeremyduncanartif...ence-software/

    The bug is in the the function thefunctionpartone line 258, it goes to a class called CountingTheFourDirections and a function called direction_value__which_direction_is_lowest__direct ion_value_array_transfer_value_to_new_array__displ aying_which_direction_is_lowest

    From there I don't know how to fix it. I have looked at it and decided to come here for some help.

    Here is the error:

    Code:
    Feeler 1 the lowest value is the west direction, so west is blocked.
    Array element zero is: 0 value
    
    
    Values with feeler 1
    I tried to take 9 steps in the north direction, but I actual took 8 steps.
    I tried to take 9 steps in the south direction, but I actual took 6 steps.
    I tried to take 9 steps in the east direction, but I actual took 6 steps.
    I tried to take 9 steps in the west direction, but I actual took 5 steps.
    
    step 1.) change feeler in that direction.
    step 2.) feel again in that direction
    note: the line A5 to A4
    
    
    Feeler 2 the lowest value is the west direction, so west is blocked.
    Array element zero is: 0 value
    
    
    Values with feeler 2
    I tried to take 9 steps in the north direction, but I actual took 6 steps.
    I tried to take 9 steps in the south direction, but I actual took 5 steps.
    I tried to take 9 steps in the east direction, but I actual took 7 steps.
    I tried to take 9 steps in the west direction, but I actual took 4 steps.
    
    
    
    
    The second feeler did something to unblock feeler 1
    
    Step 1 ended = make feeler 1 and 2 unequal - gain control.
    note: line A5 to A3
    
    <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    
    Feeler 2 the lowest value is the south direction, so south is blocked.
    Array element zero is: 0 value
    
    Feeler 2 the lowest value is the south direction, so south is blocked.
    Array element zero is: 0 value
    
    Feeler 2 the lowest value is the east direction, so east is blocked.
    Array element zero is: 1 value
    
    Feeler 2 the lowest value is the south direction, so south is blocked.
    Array element zero is: 0 value
    
    Feeler 2 the lowest value is the north, south direction, so south is blocked.
    Array element zero is: 1 value
    
    Feeler 2 the lowest value is the west direction, so west is blocked.
    Array element zero is: 1 value
    
    Feeler 2 the lowest value is the south direction, so south is blocked.
    Array element zero is: 1 value
    
    Feeler 2 the lowest value is the south direction, so south is blocked.
    Array element zero is: 1 value
    
    Feeler 2 the lowest value is the east direction, so east is blocked.
    Array element zero is: 1 value
    
    Feeler 2 the lowest value is the west direction, so west is blocked.
    Array element zero is: 0 value
    
    Values with feeler 2
    I tried to take 9 steps in the north direction, but I actual took 6 steps.
    I tried to take 9 steps in the south direction, but I actual took 6 steps.
    I tried to take 9 steps in the east direction, but I actual took 7 steps.
    I tried to take 9 steps in the west direction, but I actual took 4 steps.
    
    Step 2 ended = make feeler 1 and 2 equal - lose control.
    note: from A3, line A4 is closed.
    A3 went to A4 9 times before going to A6
    
    <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    
    Feeler 3 the lowest value is the west direction, so west is blocked.
    Array element zero is: 0 value
    
    Feeler 3 the lowest value is the north, east direction, so north is blocked.
    Array element zero is: 0 value
    
    Values with feeler 3
    I tried to take 9 steps in the north direction, but I actual took 4 steps.
    I tried to take 9 steps in the south direction, but I actual took 6 steps.
    I tried to take 9 steps in the east direction, but I actual took 4 steps.
    I tried to take 9 steps in the west direction, but I actual took 5 steps.
    
    Step 3 ended = make feeler 1 and 2 unequal - regain control.
    note: line A3 to line A6
    
    <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    
    
    
    
    Process returned 0 (0x0)   execution time : 0.281 s
    Press any key to continue.
    west is blocked for the top part, but they should be different directions.

    Run the program a few times to get the error it doesn't happen every time. And no I have not debugged the program because I'm not very good at that and thought it would be easier to ask you for help instead.

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    To say that you want someone else to debug your program for you because you find it difficult is not going to get you very far. You are in the best position to debug it since you know how it works. Try printing out the values of certain variables at strategic locations and see if they have the values you expect.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by jeremy duncan
    Run the program a few times to get the error it doesn't happen every time.
    Even with the same input? If no random numbers or threads are involved, then this non-determistic behaviour is a hint of undefined behaviour. Remember to pay attention to any warnings: ideally, your code should compile at a high warning level without warnings.

    Quote Originally Posted by algorism
    Try printing out the values of certain variables at strategic locations and see if they have the values you expect.
    jeremy duncan: you can also consider using a debugger to watch the values: you may have already been using a debugger since you wrote that the "bug is in the the function thefunctionpartone line 258".

    By the way, do you really have a function named thefunctionpartone and another named direction_value__which_direction_is_lowest__direct ion_value_array_transfer_value_to_new_array__displ aying_which_direction_is_lowest? The former is a poor name because it is not descriptive: we already can tell it is a function, and "part one" carries almost no useful information. The latter looks like it might be named decorated or something, but even if not, it is way too long and unwieldy.

    Quote Originally Posted by algorism
    You are in the best position to debug it since you know how it works.
    That said:
    Quote Originally Posted by Brian Kernighan
    Everyone knows that debugging is twice as hard as writing a program in the first place. So if you're as clever as you can be when you write it, how will you ever debug it?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Apr 2011
    Posts
    308
    The bug was I was getting a value and it couldn't change, so if it gave me the wrong value it couldn't change it. So I made a while loop and gave it a condition to change it if its the wrong value. Now the program works fine.

    Here is the new code:

    Code:
    while(1)
        {
            north_count_2 = directions_value.testing_four_directions(north_array_2, left_or_right, 4);
            south_count_2 = directions_value.testing_four_directions(south_array_2, left_or_right, 5);
            east_count_2 = directions_value.testing_four_directions(east_array_2, left_or_right, 6);
            west_count_2 = directions_value.testing_four_directions(west_array_2, left_or_right, 7);
    
            cout << '\n';
    
            feeler = 2;
            element = 1;
    
            directions_value.direction_value__which_direction_is_lowest__direction_value_array_transfer_value_to_new_array__displaying_which_direction_is_lowest(
            lowest_value_feeler_2_try_1,
            lowest_value_array_feeler_2_try_1,
            north_array_2,
            south_array_2,
            east_array_2,
            west_array_2,
            &feeler_2_lowest_value_array_direction,
            &north_count_2,
            &south_count_2,
            &east_count_2,
            &west_count_2,
            &feeler,
            &element,
            &direction_value );
            if(feeler_2_lowest_value_array_direction != *feeler_1_lowest_value_array_direction)
            {
                break;
            }
        }
    It's in the thefunctionpartone class. That other class had a different bug I found. I tried to use the codeblocks debugger by putting the red dot by that function in thefunctionpartone and then pressing step into, it gave me some idea on where to look for the eventual fix when I saw what it was doing step by step.

    I got lazy to try the debugger, but now I see it's not that difficult I won't be so spooked by it the next time.

    Thanks for the advice you 2, problem solved.

  5. #5
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528
    Quote Originally Posted by laserlight View Post

    direction_value__which_direction_is_lowest__direct ion_value_array_transfer_value_to_new_array__displ aying_which_direction_is_lowest?
    I thought I write long function/variable names until I came across this one!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 03-15-2016, 03:29 PM
  2. Replies: 4
    Last Post: 12-21-2015, 07:17 AM
  3. Replies: 2
    Last Post: 09-09-2014, 02:36 PM
  4. Replies: 1
    Last Post: 03-03-2009, 04:47 PM
  5. Replies: 18
    Last Post: 11-13-2006, 01:11 PM