Thread: Does anyone have issues with codeblocks ide

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    808

    Does anyone have issues with codeblocks ide

    a few times now the compiler (gcc) with in code blocks has thrown an error in the end out of frustration i have deleted the offending line and commented out the relevant bit of code compiled it then re added the offending line and removed the comments only to find it now compiles fine.

    the latest example of odd behavour is i have a program i have written upon changing it i discovered i had a missing closing curly bracket at the end of an if else statement. yet when i put the missing closing curly bracket in it threw an error saying expected " opening curly bracket " before closing curly bracket token. i copied and pasted it into a word editor as its a long function and difficult to see where all the brackets line up. lo and behold the "missing" closing curly bracket is there. to make it clearer below is a snipit of what i see in code blocks and what i see when i copy and paste.
    Code:
    //i see this
                        }
                    default:
                        //printf("Illegal move");
                        value_to_return = -1;
                }
            }
        }
        else
        {
            //return -1;
                  //missing } here
        return value_to_return;
    }
    
    //this is the copy and paste verstion
    
                         }
                    default:
                        //printf("Illegal move");
                        value_to_return = -1;
                }
            }
        }
        else
        {
            //return -1;
        }
        return value_to_return;
    }

    while this may be a minor annoyance to some it makes it doubly difficult o learn c when things that are visualy wrong compile correctly or right but compile as wrong.

    Just wondering if anyone else has experienced this and how it can be over come if at all.
    Last edited by cooper1200; 04-29-2019 at 05:04 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by cooper1200
    i copied and pasted it into a word editor as its a long function and difficult to see where all the brackets line up.
    I have a better solution for you: break up your functions into smaller functions that each does one thing and does it well. You will then never have such long functions (except in special cases, e.g., where they just define data).
    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

  3. #3
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Automatic indentation might help you pin-point where you have either not enough or too many curlies

    CodeBlocks: Plugins > Source code formater (AStyle)

    ... Then just go down the code until something doesn't look right
    Fact - Beethoven wrote his first symphony in C

  4. #4
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    the issue is that the "missing" curly bracket is there when i cut and paste the code
    but isn't there (visible) in codeblocks

  5. #5
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    Quote Originally Posted by laserlight View Post
    I have a better solution for you: break up your functions into smaller functions that each does one thing and does it well. You will then never have such long functions (except in special cases, e.g., where they just define data).
    here is the whole function
    Code:
    int calculate_moves(int old_x, int old_y, int new_x, int new_y)
    {
        int piece_index, value_to_return = -1;
    
        piece_index = find_piece(old_x, old_y);
        if (piece_index >= 0)
        {
            if (whos_turn == false) //whites turn
            {
                switch (old_x - new_x)
                {
                    case 0:// taking 2 pieces
                        if (new_y - old_y == 4)
                        {
                            //printf("take 2 pieces");
                            if(take_piece(old_x, old_y, new_x, new_y) == 0)//check this
                            {
                                value_to_return = 0;
                                break;
                            }
                        }
                    case 1: case -1:
                        if (new_y - old_y == 1)
                        {
                            //printf("move piece\n");
                            if(move_piece(old_x, old_y, new_x, new_y) == 0)
                            {
                                value_to_return = 0;
                                break;
                            }
                        }
                    case 2: case -2:
                        if (new_y - old_y == 2) // taking 1 piece
                        {
                            //printf("1 piece taken");
                            if (take_piece(old_x, old_y, new_x, new_y) == 0)
                            {
                                value_to_return = 0;
                                break;
                            }
                        }
                        else if (new_y - old_y == 6) // taking three pieces
                        {
                            //printf("three pieces taken\n");
                            if (take_piece(old_x, old_y, new_x, new_y) == 0)
                            {
                                value_to_return = 0;
                                break;
                            }
                        }
                    case 4: case -4://take 2 pieces in a straight line
                        if (new_y - old_y == 4)
                        {
                            //printf("2 pieces taken in a straight line");
                            if (take_piece(old_x, old_y, new_x, new_y) == 0)
                            {
                                value_to_return = 0;
                                break;
                            }
                        }
                    case 6: case -6: // take 3 pieces in a straight line
                        if (new_y - old_y == 6)
                        {
                            //printf("3 pieces taken in a straight line");
                            if (take_piece(old_x, old_y, new_x, new_y) == 0)
                            {
                                value_to_return = 0;
                                break;
                            }
                        }
                    default:
                        //printf("Illegal move");
                        value_to_return = -1;
                }
            }
            else
            {
                switch (old_x - new_x)
                {
                    case 0:// taking 2 pieces
                        if (old_y - new_y == 4)
                        {
                            //printf("take 2 pieces");
                            if(take_piece(old_x, old_y, new_x, new_y) == 0)//check this
                            {
                                value_to_return = 0;
                                break;
                            }
                        }
                    case 1: case -1:
                        if (old_y - new_y == 1)
                        {
                            //printf("move piece\n");
                            if(move_piece(old_x, old_y, new_x, new_y) == 0)
                            {
                                value_to_return = 0;
                                break;
                            }
                        }
                    case 2: case -2:
                        if (old_y - new_y == 2) // taking 1 piece
                        {
                            //printf("1 piece taken");
                            if (take_piece(old_x, old_y, new_x, new_y) == 0)
                            {
                                value_to_return = 0;
                                break;
                            }
                        }
                        else if (old_y - new_y == 6) // taking three pieces
                        {
                            //printf("three pieces taken\n");
                            if (take_piece(old_x, old_y, new_x, new_y) == 0)
                            {
                                value_to_return = 0;
                                break;
                            }
                        }
                    case 4: case -4://take 2 pieces in a straight line
                        if (old_y - new_y == 4)
                        {
                            //printf("2 pieces taken in a straight line");
                            if (take_piece(old_x, old_y, new_x, new_y) == 0)
                            {
                                value_to_return = 0;
                                break;
                            }
                        }
                    case 6: case -6: // take 3 pieces in a straight line
                        if (old_y - new_y == 6)
                        {
                            //printf("3 pieces taken in a straight line");
                            if (take_piece(old_x, old_y, new_x, new_y) == 0)
                            {
                                value_to_return = 0;
                                break;
                            }
                        }
                    default:
                        //printf("Illegal move");
                        value_to_return = -1;
                }
            }
        }
        else
        {
            value_to_return = -1;
        }
        return value_to_return;
    }
    the bit that makes it so long is the switch case statements but if i use if else if and else statements it then runs out of space to the right without having to scroll

    for example if i replaced the case -1: case 1 with an else if it would be

    Code:
    else if (((old_x - new_x == 0) || (old_x - new_x == 1)) && (new_y - old_y == 1)))
    with out the new_y part of the evaluation i just replace 1 line of code with another

    so i cant see how to make it any shorter.
    coop

  6. #6
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    scratch that i dont need to ask whos turn it is so that eliminates the need for the second switch statement as it doesn't care whose go it is or the "piece index". all it needs is the switch statement. Sorry i was in the prosess of modifying the code when i noticed the closing curly bracket problem
    coop
    Last edited by cooper1200; 04-29-2019 at 06:22 AM. Reason: better explination

  7. #7
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    Quote Originally Posted by Click_here View Post
    Automatic indentation might help you pin-point where you have either not enough or too many curlies

    CodeBlocks: Plugins > Source code formater (AStyle)

    ... Then just go down the code until something doesn't look right
    as a side note am i mixing styles as i prefer each curly bracket on its own line (both opening and closing) however when i tried the (AStyle) it left all my indentation the same as far as i could see but put the case statements on separate lines

  8. #8
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Quote Originally Posted by cooper1200 View Post
    as a side note am i mixing styles as i prefer each curly bracket on its own line (both opening and closing) however when i tried the (AStyle) it left all my indentation the same as far as i could see but put the case statements on separate lines
    I like AStyle because I do a lot of C# (and that is the default style VS gives you) - The correct style is whatever your manager/company tells you to do!

    Just out of curiousity, if this returns not 0...
    Code:
    if (take_piece(old_x, old_y, new_x, new_y) == 0)
    ... it doesn't hit the "break" and falls through to default... Is that on purpose? It's probably what you want, but in my experience fall throughs are usually an accident...


    I can definately see that the if statement could be a function return
    Code:
    int new_move(int change_in_xy, old_x, old_y, new_x, new_y)
    {
      ...
    }
    It makes it easier to debug and change anything in this process - Imagine if you need to add a log to every move (or something else)... It would suck to have to go through each case pasting the function...

    You could even do the inner if statement as another function, as it is just copy/paste...

    Taking what laserlight said is probably a good idea, because lots of small functions are better than one massive function when it comes to maintaining code and unit testing down the track.

    What I have found is it doesn't matter how well the code works, it's behaviour will probably change over the lifetime of the programme and good code is one that can be changed and tested easily. And I think that laserlight's advice is one from lots of experience.
    Fact - Beethoven wrote his first symphony in C

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It looks like you found a way to eliminate one of the switch statements, but if not, my first suggestion would be to move them to their own functions.

    The case labels should be on their own lines. You can make use of an absolute value function/macro to avoid the pairs of case labels to begin with.

    You can combine some of the conditions with || or && (break up long lines into multiple physical lines if it helps readability)
    Last edited by laserlight; 04-29-2019 at 07:04 AM.
    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

  10. #10
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    Quote Originally Posted by Click_here View Post
    I like AStyle because I do a lot of C# (and that is the default style VS gives you) - The correct style is whatever your manager/company tells you to do!

    Just out of curiousity, if this returns not 0...
    Code:
    if (take_piece(old_x, old_y, new_x, new_y) == 0)
    ... it doesn't hit the "break" and falls through to default... Is that on purpose? It's probably what you want, but in my experience fall throughs are usually an accident...
    Yes its deliberate as the function take piece returns either 0 if everything went ok ie it found the piece(s) to take and arrived at the new co-ordinates or -1 if it couldn't find the path to take. would it be better to have an if else statement there instead and have the else set value_to_return to -1 rather than setting it at the time of declaring the variable.

    Quote Originally Posted by Click_here;1285422I can definately see that the if statement could be a function return
    [code
    int new_move(int change_in_xy, old_x, old_y, new_x, new_y)
    {
    ...
    }
    [/code]
    It makes it easier to debug and change anything in this process - Imagine if you need to add a log to every move (or something else)... It would suck to have to go through each case pasting the function...

    You could even do the inner if statement as another function, as it is just copy/paste...
    wouldnt i still have to test the returned int to see if it completed ok????


    many thanks
    coop

  11. #11
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    Quote Originally Posted by laserlight View Post
    You can make use of an absolute value function/macro to avoid the pairs of case labels to begin with.
    sorry im not sure what you mean. Do you mean a #deffine case+-1 case -1: case 1

    QUOTE=laserlight;1285423]You can combine some of the conditions with || or && (break up long lines into multiple physical lines if it helps readability)[/QUOTE]
    how do i break up lines of code. for example how would i break up the following so it was over 2 or more lines.
    Code:
    else if (((old_x - new_x == 0) || (old_x - new_x == 1)) && (new_y - old_y == 1)))
    many thanks
    coop

  12. #12
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Code::Blocks IDE is not a compiler; you need to learn the difference between types of errors if you ever wish to stop being an C newbie.
    compiler errors
    linker errors
    run-time errors
    and other types of errors

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  13. #13
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    hence why part of my opening statment was "the compiler (gcc) with in codeblocks"

  14. #14
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by cooper1200 View Post
    hence why part of my opening statment was "the compiler (gcc) with in codeblocks"
    So, you still have a lot to learn because that statement means you have no idea of the correct state.

    The compiler shipped with Code::Blocks or the compiler being used by Code::Blocks are more accurate.

    There is no compiler within Code::Blocks!

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by cooper1200
    sorry im not sure what you mean. Do you mean a #deffine case+-1 case -1: case 1
    No. Use abs from <stdlib.h>. If you want to define your own absolute value macro, then it should take an integer and result in the non-negative value of that integer.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. codeblocks and wxwidget
    By hayview in forum C Programming
    Replies: 5
    Last Post: 10-10-2011, 03:03 AM
  2. Need help with codeblocks
    By SilverClif in forum Windows Programming
    Replies: 6
    Last Post: 05-24-2011, 05:12 AM
  3. using codeblocks
    By torquemada in forum Tech Board
    Replies: 7
    Last Post: 04-20-2011, 08:57 PM
  4. Codeblocks.
    By Kitt3n in forum C++ Programming
    Replies: 5
    Last Post: 05-16-2010, 01:50 PM
  5. library issues in CodeBlocks
    By everlearnin in forum C++ Programming
    Replies: 0
    Last Post: 06-02-2009, 08:44 PM

Tags for this Thread