Ok, so over the summer I started making a simple tic-tac-toe program in my spare time to get some practice with pointers, structs, and programming in C in general. Overall, the program has been going great, no major issues. However, there is one area I am having a bit of confusion.
When I started this project, at the beginning of the summer, I had only a small understanding of C, with my first CS class being in C++. As such, I was not too familiar with how to read input from stdin, so I ended up using getchar(). Now, I am trying to change my input to use scanf() instead. At one point in particular, this is causing some small issues. Reference the code block below:
Code:
int main(int argc, char **argv)
{
if (system("cls"))
system("clear");
printf("TicTacToe v1.0\nBy Daniel Bauer\n\n");
printf("Number of games to play: ");
int games, i;
if(scanf("%d", &games) == 0)
{
printf("Input error, expected integer\n");
return 0;
}
for(i = 0; i < games; i++)
{
if (system("cls"))
system("clear");
board *gameBoard = initializeBoard( );
while(gameBoard->human != 'X' && gameBoard->human != 'O')
{
printf("Pick X or O: ");
scanf("%c", &gameBoard->human);
gameBoard->human = toupper(gameBoard->human);
}
if(gameBoard->human == 'X')
gameBoard->computer = 'O';
else
gameBoard->computer = 'X';
...
This part of code is supposed to read in the number of games the user wants to play and then whether the player wants to be X or O. The problem is that when the program gets to asking the player X or O, it will print "Pick X or O: Pick X or O: ", when I would prefer it only print that once.
Does anyone here know what might be causing this? Any help with fixing my code or a workaround would be appreciated.
And if for whatever reason you need to see more of the code, just let me know!