Sick of me yet?

This is a discussion on Sick of me yet? within the C Programming forums, part of the General Programming Boards category; Heh. I came to school and got my program to work, thankfully. (novacain, thanks for pointing out that == thing...I ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    11

    Talking Sick of me yet?

    Heh.

    I came to school and got my program to work, thankfully. (novacain, thanks for pointing out that == thing...I must be retarted or something Oh, and yeah, its an assignment, though far from being done, as you can tell. I know what I want to do as far as the code goes, I went and wrote out a flow chart and everything, but I keep coming up with errors that I dunno how to deal with.) But now I've got a small problem with my function. Its almost the exact same code as before:

    /*Include Statements*/
    #include <stdio.h>
    #include <string.h>

    /*Data Structures*/
    struct GAME
    {
    char location[3][3];
    };

    /*Function Prototypes*/
    void displayboard(GAME pos);
    void setblank(GAME positionfunct);

    /*Main Function*/
    int main(void)
    {
    /*Variable Declaration*/
    GAME position;

    /*Sets all of the Mark Locations to a Blank Space*/
    setblank(position);

    displayboard(position);

    return 0;
    }

    void setblank(GAME positionfunct)
    {
    int a, b;
    for(a=0; a<3; a++)
    {
    for(b=0; b<3; b++)
    {

    positionfunct.location[a][b] = ' ';
    }
    }

    return;
    }

    void displayboard(GAME pos)
    {
    printf("\n\t\t\t Columns");
    printf("\n\t\t\t 1 2 3\n");
    printf("\n\t\t\t | |");
    printf("\n\t\t\t 1 %c | %c | %c", pos.location[0][0], pos.location[1][0], pos.location[2][0]);
    printf("\n\t\t\t | |");
    printf("\n\t\t\tR =================");
    printf("\n\t\t\to | |");
    printf("\n\t\t\tw 2 %c | %c | %c", pos.location[0][1], pos.location[1][1], pos.location[2][1]);
    printf("\n\t\t\ts | |");
    printf("\n\t\t\t =================");
    printf("\n\t\t\t | |");
    printf("\n\t\t\t 3 %c | %c | %c", pos.location[0][2], pos.location[1][2], pos.location[2][2]);
    printf("\n\t\t\t | |\n");

    return;
    }


    ...but now, like I said, I've got a problem with my function somewhere. The loop that I have in my setblank function to set all the positions to ' ' isn't working. If I take the loop out of the function and stick it into main, it works perfectly. But if I leave it in its function, it doesn't set the locations to a blank space, it sets it to this 'Ęd'. The only thing that I can think of, is that somewhere along the way, when I'm passing the variable from main to my function, its getting messed up. I know that arrays are passed by location by default, so I didn't think I'd need to use pointers. I went and tried it with pointers, thinking that maybe since it was a new variable type (GAME), it would require them. It just came up with a few errors, so I don't think that was it. I'd like to have the loops in their own function, though its really not needed, because I only use it once through every run. If you guys(and girls?) have any idea, please let me know, if not I'll just stick it into the main function and lose a point or two. ;p .

    Thanks for your time,

    - Pat

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,498
    The first problem you have, is that you're compiling this code using C++ compiler

    How can I tell?
    Because the struct GAME syntax (more specifically your use of it later on without the struct keyword is only valid in C++).

    The other problem is that you've buried your array inside a structure. Now whilst arrays are passed as pointers, arrays inside structs are copied, so any changes in setblank are not copied back to main.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    /*Data Structures*/
    /* Without changes elsewhere, struct GAME is only valid in C++ */
    typedef struct GAME {
        char location[3][3];
    } GAME;
    
    /*Function Prototypes*/
    void displayboard(GAME pos);
    void setblank(GAME *positionfunct); //!LOOK
    
    /*Main Function*/
    int main(void) {
        /*Variable Declaration*/
        GAME position;
        /*Sets all of the Mark Locations to a Blank Space*/
        setblank( &position );      /* Pass a pointer to the structure */
        displayboard( position );
        return 0;
    }
    
    void setblank(GAME *positionfunct) { //!LOOK
        int a, b;
        for(a=0; a<3; a++) {
            for(b=0; b<3; b++) {
                positionfunct->location[a][b] = '@'; //! a visible space for tests
            }
        }
        return;
    }
    
    void displayboard(GAME pos) {
        printf( "\n\t\t\t         Columns" );
        printf( "\n\t\t\t      1     2     3\n" );
        printf( "\n\t\t\t         |     |" );
        printf( "\n\t\t\t  1   %c  |  %c  |  %c",
            pos.location[0][0], pos.location[1][0], pos.location[2][0] );
        printf( "\n\t\t\t         |     |" );
        printf( "\n\t\t\tR   =================" );
        printf( "\n\t\t\to        |     |" );
        printf( "\n\t\t\tw 2   %c  |  %c  |  %c",
            pos.location[0][1], pos.location[1][1], pos.location[2][1] );
        printf( "\n\t\t\ts        |     |" );
        printf( "\n\t\t\t    =================" );
        printf( "\n\t\t\t         |     |" );
        printf( "\n\t\t\t  3   %c  |  %c  |  %c",
            pos.location[0][2], pos.location[1][2], pos.location[2][2] );
        printf( "\n\t\t\t         |     |\n" );
        return;
    }

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    11

    Ahh...

    Cool, thanks. I didn't know that there was a difference between declaring structs in C and ++...

    I tried it the way you did, and in the one line:

    positionfunct->location[a][b] = '@'; //! a visible space for tests

    if I use the -> like you have, it gives me an error saying "illegal indirection". If I use a . like I was before, it says "left of '.location' must have class/struct/union type".

    Oh, and I changed the Structure declaration to what you have...

    - Pat

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    I compiled Salem's code without errors. Maybe you forgot to change the prototype for the function at the top.

  5. #5
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,856
    No problem.
    The == and = thing is almost as common as the loop with a ;
    ie
    for(i=0;i<10;i++);
    Sometimes they are the hardest to spot as you are looking for something complex.

    I would use an int array for this and a hash define. This would also allow you to add rows/columns.
    ie
    #define x 1
    #define o 0

    Code:
    //test for a winner
    for(j=0;j<3;j++)
    {
        if(Array[j][0]+Array[j][1]+Array[j][2])==3)
        //three in a row
        {
              sprintf(sBuffer,"The X player wins!");
        }
    }
    Just my 2 cents.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Are you sick of Gaagle.com ?
    By kryptkat in forum A Brief History of Cprogramming.com
    Replies: 44
    Last Post: 01-26-2006, 06:54 PM
  2. This is a sick error
    By maxorator in forum Windows Programming
    Replies: 7
    Last Post: 11-17-2005, 11:58 AM
  3. Windows explorer is sick (win98se)
    By jawwadalam in forum Tech Board
    Replies: 15
    Last Post: 01-03-2003, 08:39 PM
  4. SICK of this
    By hermit in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 06-22-2002, 10:13 PM
  5. i'm sick of this... so sick.
    By Aran in forum A Brief History of Cprogramming.com
    Replies: 19
    Last Post: 04-10-2002, 08:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21