I have a program that is meant to continue looping as long as an entered character is 'y', but my code exits after one execution of the do-while loop.
int main()
{
char cont;
do {
printf("Would you like to continue (y/n)?: ");
scanf_s("%c", &cont, sizeof(cont));
} while (cont == 'y');
}
This will take in one key press and either exit or continue, as it's supposed to, but then immediately exit without waiting for more key presses.
scanfis discouraged. When reading a line of input use a line-oriented input function likefgets()or POSIXgetline(), Then it'schar buf[128]; do printf("continue (y/n)?: "); if (!fgets (buf, sizeof buf, stdin)) { puts ("(user canceled input)"); return 1; }; } while (*buf == 'y');