Scanf skips every other while loop in C

前端 未结 10 2138
清歌不尽
清歌不尽 2020-11-22 01:55

I\'m trying to develop a simple text-based hangman game, and the main game loop starts with a prompt to enter a guess at each letter, then goes on to check if the letter is

10条回答
  •  独厮守ぢ
    2020-11-22 02:07

    Newlines.

    The first time through the loop, scanf() reads the character. Then it reads the newline. Then it reads the next character; repeat.

    How to fix?

    I seldom use scanf(), but if you use a format string "%.1s", it should skip white space (including newlines) and then read a non-white space character. However, it will be expecting a character array rather than a single character:

    char ibuff[2];
    
    while ((scanf("%.1s", ibuff) == 1)
    {
        ...
    }
    

提交回复
热议问题