-
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 :p 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
-
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;
}
-
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
-
I compiled Salem's code without errors. Maybe you forgot to change the prototype for the function at the top.
-
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.