Answer the question
In order to leave comments, you need to log in
Why is the 'menu' shown 2 times?
Hello! Started learning the C language for programming robots. I decided to make the game "Hangman", but there is a small problem. For some reason the menu is shown twice after I enter something. I can't understand the reason.
Here's what that piece of code looks like -
void main() {
char choice;
printf("Hello! You got into the 'Hangman' game\n");
while (choice != '0') {
printf("\n0. Exit\n1. Phone brands\n2. Programming languages\n3. Kinds of sports\n");
printf("Enter what you want to guess: ");
scanf("%c", &choice);
if (choice == '0') {
printf("\nGoodBye...");
break;
} else if (choice == '1')
printf(SevenAttempt);
}
}
Answer the question
In order to leave comments, you need to log in
For some reason the menu is shown twice after I enter something.
scanf("%c", &choice);
scanf(" %c%*[^\n]", &choice);
%c
swallows all whitespace characters, %*[^\n]
swallows the tail of the line after the first non-whitespace character read.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question