[Solved] I’m trying to make a very simple hangman program work. The while loop isn’t working [closed]


When the scanf("%i", &choice); statement is executed, it puts the integer entered by the user, into the int variable choice. However, it also leaves the newline character in the input buffer causing the user input to get thrown off. When the scanf("%c\n", &guess); statement is executed, the next character entered is placed on the input buffer, but the %c only causes one character (the newline character) to be read from the buffer, while the character the user entered is still on the buffer. Because there’s a newline character after the %c in the format string, the newline character that would have followed it is discarded (because it matches the next newline). On subsequent iterations the previous character still on the buffer is then read.

The recommended solution is to insert a single space before the %c like this: scanf(" %c", &guess). When scanf is executed, the single space will cause it to ignore all other whitespace characters until the first non-whitespace character, which in this case means the newline still on the buffer will be read and ignored.

1

solved I’m trying to make a very simple hangman program work. The while loop isn’t working [closed]