Now I want to print a chessboard to the console. The function takes a board as argument and should print a dot for empty square and a piece char for piece.
but I have some problems.
First the code does not compile so I don't know if it works or not. The programming should also be done in C.
First the code does not compile so I don't know if it works or not.
Thank you for not passing onto us the information that the compiler is giving you about why it won't compile. This is so much more fun when we have to play guessing games.
Line 19: Where is sq initialized? Hint: It's garbage.
370 posts and you have not learned to use code tags? http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
If you're not going to make the slightest bit of effort to make your posts readable, why should we spend the slightest bit of effort helping you?
I will not respond further until you apply code tags.
I'm afraid I have some problems that I haven't solved yet.
¿What problems?
First, how should I call this print board function?
Let us take a look at the function prototype void printboard(struct position *p)
It is a void function so that usually implies it goes on a line by itself: printboard();
It receives a pointer to a struct position variable. I am going to pretend we have one called bob for now. printboard(&bob); //& gets the address of a variable (which is exactly what a pointer is)
And that is it. Done finished. Complete. With calling the function that is.
On to getting bob to come into existence... struct position bob;
That is all there is to giving birth to bob.
bob has not learned anything yet in his life though. He is still just a random collection of leftover memory. We need to fill him up with some bits. So if you use a for loop to set every spot of bob.board to '.' you would be in business.
I tried to define square as a squareIndex = 8*rankIndex + fileIndex but does not work.
Did you do that in the innermost for loop? Or when the variable is declared?
Inside the for loop it would change and produce the index for every spot on the board since rankIndex and fileIndex would be changing.
If you did it when the variable was declared outside of the for loops, then that just happens once and it will never change.
What Thomas1965 was trying to get you to see is you need something to change sq inside the loop. I recommend simply incrementing sq inside the loop like ++sq;