Scanf skips every other while loop in C

前端 未结 10 2132
清歌不尽
清歌不尽 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:08

    Jim and Jonathan have it right.

    To get your scanf line to do what you want (consume the newline character w/o putting it in the buffer) I'd change it to

    scanf("%c\n", ¤tGuess);
    

    (note the \n)

    The error handling on this is atrocious though. At the least you should check the return value from scanf against 1, and ignore the input (with a warning) if it doesn't return that.

提交回复
热议问题