-3

I'm developing a TicTacToe in C, but I'm having trouble displaying the name of the players.
Every time a player has to make a move, I display "It's your turn, %s!".
But from the second iteration, the player name is empty.

After some tests I figured that when I comment line 56 the problem doesn't happen.

Console output:

Starting...

Type your username (20 characters): Paulo

Type your username (20 characters): Edson


   A   B   C 
0 ___|___|___
1 ___|___|___
2 ___|___|___


It's your turn, Paulo!

Choose a position (Ex.: B2, A0, etc): A2

Picked: A2


   A   B   C 
0 ___|___|___
1 ___|___|___
2 ___|___|___


It's your turn, !

Choose a position (Ex.: B2, A0, etc): A3

Picked: A3


   A   B   C 
0 ___|___|___
1 ___|___|___
2 ___|___|___


It's your turn, !

Choose a position (Ex.: B2, A0, etc): ^C

This is my code:

#include <stdio.h>
#include <string.h>

void pickposition(char * position) {
  printf("\nChoose a position (Ex.: B2, A0, etc): ");
  scanf(" %2s", position);
}

void printtable(char table[3][3]) {
  printf("\n\n");

  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
        char c = table[i][j];
        if (c != 'O' && c != 'X') {
            table[i][j] = '_';
        }
    }
  }

  printf("   A   B   C \n");
  printf("0 _%c_|_%c_|_%c_\n", table[0][0], table[0][1], table[0][2]);
  printf("1 _%c_|_%c_|_%c_\n", table[1][0], table[1][1], table[1][2]);
  printf("2 _%c_|_%c_|_%c_\n", table[2][0], table[2][1], table[2][2]);
  printf("\n\n");
}

void pickusername(char *name) {
  printf("\nType your username (20 characters): ");
  scanf("%20s", name);
}

int main() {
  printf("\nStarting...\n");
  char table[3][3];

  char player1[20];
  char player2[20];

  pickusername(player1);
  pickusername(player2);

  int next = 1;
  int counter = 0;
  while (1) {
    char nextname[20];
    printtable(table);
    if (next) {
        strcpy(nextname, player1);
    } else {
        strcpy(nextname, player2);
    }
    printf("It's your turn, %s!\n", nextname);

    char positionpicked[2];
    pickposition(positionpicked);
    printf("\nPicked: %s\n", positionpicked);

    counter--;
    if (counter == -20) {
        break;
    }
  }
}

PS.: I know that the next is always 1, that is just a draft.

Edson Passos
  • 117
  • 10
  • 1
    Please don't post pictues of text, but post text as text. And post your code here, not as an external link. You may [edit] your question. Also read this: [ask] – Jabberwocky Nov 02 '18 at 11:12
  • 1
    Probably: `posicaoescolhida[2];` -> `posicaoescolhida[3];`. You need space for the NUL string terminator, read the chapter dealing with strings in your C text book. There may be more problem elsewhere. – Jabberwocky Nov 02 '18 at 11:14
  • The second part of my first comment still stands... – Jabberwocky Nov 02 '18 at 11:18
  • @Jabberwocky any specific topics? – Edson Passos Nov 02 '18 at 11:20
  • @Jabberwocky `posicaoescolhida[3];` this solved, thanks! – Edson Passos Nov 02 '18 at 11:24
  • 1
    Always paste code and output please. I did this for you now. Next time please do it yourself. – alk Nov 02 '18 at 11:30
  • @alk Thank you. I don't have experience with markdown. – Edson Passos Nov 02 '18 at 11:32
  • 1
    C-"strings" always require one more character then they should hold. It stores the end-of-the-string-marker, the `0`-terminator. So assuming a 20 character buffer then this `scanf("%20s", nome);` ought be `scanf("%19s", nome);` – alk Nov 02 '18 at 11:34
  • 2
    Also it might be sub-optimal do provide non-English "speaking" code to an English speaking community. – alk Nov 02 '18 at 11:36

1 Answers1

1

When I ran your program it was working fine for me. But there is a small correction required in the size of positionpicked array. You are asking the user to input two character(A0/B1) but positionpicked has only 2 elements which means that there is no allocated memory to store '\0'. So the size of the positionpicked should be increased to 3.

This can also be the cause of the problem(i am not sure but most probably it will be) as there is chance this NULL character will overwrite the player1[0].

During first iteration:-

player1 -> Pablo0
player2 -> Edson0
positionpicked -> A20 /* Since positionpicked can only store 2 bytes the 0('\0')
                         is stored in the adjacent memory location */

During second iteration:-

player1 -> 0ablo0    /* Which will print a null string */