user3242754
user3242754

Reputation: 21

Simple C Tic Tac Toe Input Errors

I'm currently attempting to learn C, and decided to make a simple Tic Tac Toe "game".

I'm not trying to do anything fancy, I only want to take a user's position and their given character and put it in my grid. I haven't started with arrays or anything, so I put together a grid using printf.

The issue comes in when I attempt to actually run the game, the character doesn't work. I can type in the correct input, but it just makes that position go blank. How do I make this switch statement store the character where I want it?

    #include <stdio.h>
    #include <stdlib.h>

    int main()
    {
    // Initialize and define all variables and characters
    char c1='.', c2='.', c3='.', c4='.', c5='.', c6='.', c7='.', c8='.', c9='.'; // Board     characters
    char given; // User-input character
    int pos; // User-input position for the character
    int turnCount; // Gives a limit on game length

    for (turnCount = 0; turnCount < 9; turnCount++)
    {
        // Display lines
        printf("%c %c %c   1 2 3\n", c1, c2, c3); // First line
        printf("%c %c %c   4 5 6\n", c4, c5, c6); // Second line
        printf("%c %c %c   7 8 9\n", c7, c8, c9); // Third line

        // Asks for input, scans input for position and character to use
        printf("Choose a position and character, without spaces.\n");
        scanf_s("%i%c", &pos, &given);

        // Matches the input position with a character, defines the character
        switch (pos)
        {
        case 1: c1 = given; break;
        case 2: c2 = given; break;
        case 3: c3 = given; break;
        case 4: c4 = given; break;
        case 5: c5 = given; break;
        case 6: c6 = given; break;
        case 7: c7 = given; break;
        case 8: c8 = given; break;
        case 9: c9 = given; break;
        } // End switch
    } // End for - game should be over
    return 0;
}

Upvotes: 2

Views: 215

Answers (2)

George
George

Reputation: 416

Replace scanf_s("%i%c", &pos, &given); with scanf("%i%c", &pos, &given); will solve your problem. This is because scanf_s requires a buffer size for character inputs, whereas scanf does not. An alternative is to include the buffer size: scanf_s("%i%c", &pos, &given,1);.

I've tested both and they work on my windows system.

Edit: Just for fun, I implemented this using arrays. It's a bit cleaner this way.

#include <stdio.h>
#include <stdlib.h>

int main()
{
    // Initialize and define all variables and characters
    char gameBoard[3][3];
    char given; // User-input character
    int pos; // User-input position for the character
    int turnCount; // Gives a limit on game length
    int i;
    //set the gameboard to all '.'
    for(i=0;i<9;i++)
    {
        gameBoard[i/3][i%3] = '.';
    }

    for (turnCount = 0; turnCount < 9; turnCount++)
    {
        // Display lines
        printf("%c %c %c   1 2 3\n", gameBoard[0][0], gameBoard[0][1], gameBoard[0][2]); // First line
        printf("%c %c %c   4 5 6\n", gameBoard[1][0], gameBoard[1][1], gameBoard[1][2]); // Second line
        printf("%c %c %c   7 8 9\n", gameBoard[2][0], gameBoard[2][1], gameBoard[2][2]); // Third line

        // Asks for input, scans input for position and character to use
        printf("Choose a position and character, without spaces.\n");
        scanf_s("%i%c", &pos, &given,1);

        // sets the character on the gameboard.
        gameBoard[(pos-1)/3][(pos-1)%3] = given;
    } // End for - game should be over
    return 0;
}

Upvotes: 3

julumme
julumme

Reputation: 2366

This code seems to work on Linux if I change the scanf_s to scanf. I don't have Windows system here to test it, but I assume something is not ok with the scanf_s function call.

According to here: http://faculty.edcc.edu/paul.bladek/CS131/scanf_s.htm:

Unlike scanf,  scanf_s  requires the buffer size to be specified for all input 
parameters of type c, C, s, S, or [. The buffer size is passed as an additional 
parameter immediately following the pointer to the buffer or variable

So maybe you should try your scanf_s call like this:

scanf_s("%i%c",&pos, &given, 1);

I can't verify this now, though...

Upvotes: 3

Related Questions