Why does the message print twice?

后端 未结 4 1327
灰色年华
灰色年华 2021-01-15 01:29

I am creating a simple Tic Tac Toe for C, and here is a particular function which I am having a problem with. This is supposed to let the user select \'X\' or \'O\', and fo

4条回答
  •  不要未来只要你来
    2021-01-15 02:17

    You have a newline in your input buffer.

    When you press a character which is not [xX] and not [oO] and follow it with a newline. getchar actually gets to see 2 characters(the newline and the invalid character)

    You may want to use fgets instead of relying on character input and ignoring newlines with 2 getchar() calls everytime.

提交回复
热议问题