Thread: what is wrong with this function

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

    what is wrong with this function

    i have a function that looks at a specific element in the board array (x and y co-ordinate) and returns true or false if the element has a capital W or B
    Code:
    ool check_king(char board[][ROW_MAX + 1][MESSAGE], int current_player, int x_coordinate, int y_coordinate)
    {
        //char *p_king_label = current_player ? "B" : "W";
        //printf("%s\n", p_king_label);
    
        //return ((board[x_coordinate][y_coordinate] == 'W') || (board[x_coordinate][y_coordinate] == 'B')) ? true : false;
        //check this works properly once we have a king if not do strcat outside of the return by adding current player as a parameter
        printf("w%sW", board[x_coordinate][y_coordinate]);
        return ((strcmp(board[x_coordinate][y_coordinate], "W") == 0) ||
               ((strcmp(board[x_coordinate][y_coordinate], "B") == 0))) ? true : false;
    }
    even hard coding them in rather than using a pointer doesnt work. and the printf statement doesn't print any thing.

    many thanks
    coop

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by cooper1200
    even hard coding them in rather than using a pointer doesnt work. and the printf statement doesn't print any thing.
    I compiled and ran this program:
    Code:
    #include <stdbool.h>
    #include <stdio.h>
    #include <string.h>
    
    #define COL_MAX 2
    #define ROW_MAX 2
    #define MESSAGE 10
    
    bool check_king(char board[][ROW_MAX + 1][MESSAGE], int current_player, int x_coordinate, int y_coordinate)
    {
        //char *p_king_label = current_player ? "B" : "W";
        //printf("%s\n", p_king_label);
     
        //return ((board[x_coordinate][y_coordinate] == 'W') || (board[x_coordinate][y_coordinate] == 'B')) ? true : false;
        //check this works properly once we have a king if not do strcat outside of the return by adding current player as a parameter
        printf("w%sW", board[x_coordinate][y_coordinate]);
        return ((strcmp(board[x_coordinate][y_coordinate], "W") == 0) ||
               ((strcmp(board[x_coordinate][y_coordinate], "B") == 0))) ? true : false;
    }
    
    int main(int argc, char *argv[])
    {
        char board[COL_MAX + 1][ROW_MAX + 1][MESSAGE] = {{"B", "w"}, {"b", "w"}};
        if (check_king(board, 0, 0, 0))
        {
            printf("\ncheck_king returned true\n");
        }
        else
        {
            printf("\ncheck_king returned false\n");
        }
        return 0;
    }
    It compiled without any errors or warnings, and I received this output:
    Code:
    wBW
    check_king returned true
    So... what's wrong with this function? Who knows?

    In the future, I won't bother doing this. I'll just ask you to refer to my forum signature.

    By the way, this:
    Code:
    return ((strcmp(board[x_coordinate][y_coordinate], "W") == 0) ||
           ((strcmp(board[x_coordinate][y_coordinate], "B") == 0))) ? true : false;
    can be simplified to:
    Code:
    return (strcmp(board[x_coordinate][y_coordinate], "W") == 0) ||
           (strcmp(board[x_coordinate][y_coordinate], "B") == 0);
    I understand the concern about not getting the order of precedence right, but when you're using two sets of parentheses where one set will do, you really need to start thinking of reducing your parentheses usage: at some point they begin to obfuscate rather than illuminate the code.
    Last edited by laserlight; 05-08-2019 at 05:54 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

  3. #3
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    why don't i need the true and false bit i saw some use the or operator before in their code to return different values is it as simple as each statement is either true or false effectively therefore if one or the other is true its true or if both are false its false.
    or is there more to it than that.
    coop

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The result of the || operator is already a boolean value. Granted, it is technically an int with the value of either 1 or 0, but that doesn't make any difference.

    Put it this way: whenever you see an expression of this form:
    Code:
    x ? true : false
    ask yourself: is x already a boolean value? If so, simplify to just x. Otherwise, there's probably a comparison you can introduce to turn it into a boolean value.

    Likewise, if it is of this form:
    Code:
    x ? false : true
    you can simplify to !x or introduce a comparison.
    Last edited by laserlight; 05-08-2019 at 06:52 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

  5. #5
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    ok i think its the escape code causing issues because if i put printf("\nw%sW\n", board[x_coordinate][y_coordinate]) instead of printf("w%sW", board[x_coordinate][y_coordinate]) i know see the output on the screen
    coop

  6. #6
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    Quote Originally Posted by laserlight View Post
    Likewise, if it is of this form:
    Code:
    x ? false : true
    you can simplify to !x or introduce a comparison.
    so instead of
    Code:
    position = p_position == NULL ? false :  true;
    return position;
    i can have
    Code:
    position = !p_position == NULL;
    return position;
    or do away with the position variable and just return !p_position == NULL;
    coop
    Last edited by cooper1200; 05-08-2019 at 07:52 AM.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I would write:
    Code:
    return p_position != NULL;
    Last edited by laserlight; 05-08-2019 at 08:03 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

  8. #8
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    on the topic of writing things properly is it ok to do the following
    Code:
    if (x == 1)
    {
         //do stuff
         return true;
    }
    return false;
    or should i use an else statement.
    coop

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    That's fine.
    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. What's wrong with my cos(x) function
    By jjohan in forum C Programming
    Replies: 22
    Last Post: 09-19-2014, 04:09 PM
  2. what is wrong with this function?
    By rodrigorules in forum C++ Programming
    Replies: 0
    Last Post: 11-22-2009, 05:53 AM
  3. What's wrong with this function ?
    By johngav in forum C Programming
    Replies: 22
    Last Post: 06-24-2009, 12:09 PM
  4. wrong wrong with my xor function?
    By Anddos in forum C++ Programming
    Replies: 5
    Last Post: 04-26-2009, 01:38 PM
  5. Something wrong with my function!?
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 02-02-2002, 08:29 AM

Tags for this Thread