Search code examples
c++loopsduplicatesdo-whilegetchar

C++ do...whille loop duplicate prints with getchar function


The code below is printing "Menu" twice when input is different from 3.

do{
    puts("Menu");
    option = getchar();
}
while (option != '3');

Solution

  • A quick fix:

    do{ puts("Menu"); std::cin >> option; } while (option != '3');