The code below is printing "Menu" twice when input is different from 3.
do{
puts("Menu");
option = getchar();
}
while (option != '3');
The code below is printing "Menu" twice when input is different from 3.
do{
puts("Menu");
option = getchar();
}
while (option != '3');
You just need to flush your stdin. When your program reaches getChar, it finds the newline character, and processes it as an input character.
if your not set on using getChar()
to retrieve the input a better option would probably be to use std::cin
, as it deals with white space such as this more intelligently
You can also flush the input by adding the line fseek(stdin,0,SEEK_END);
right before you retrieve the character from the input.
or you can run an if statement to check if the character retrieved is newline
if ( option != '\n' ) {
...
}
also see this answer Using getchar() on c gets the 'Enter' after input