Scanf skips every other while loop in C

前端 未结 10 2140
清歌不尽
清歌不尽 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条回答
  •  萌比男神i
    2020-11-22 02:23

    I see a couple of things in your code:

    1. scanf returns the number of items it read. You will probably want to handle the cases where it returns 0 or EOF.
    2. My guess would be that the user is hitting letter + Enter and you're getting the newline as the second character. An easy way to check would be to add a debugging printf statement to show what character was entered.
    3. Your code will only match the first occurrence of a match letter, i.e. if the word was "test" and the user entered 't', your code would only match the first 't', not both. You need to adjust your first loop to handle this.

提交回复
热议问题