Hi,

On Wed, Apr 16, 2025 at 12:06:25PM +0000, NRK wrote:
> The whole line needs to be read, even when the first char is `y|Y`.
> Also, getchar may return EOF before newline leading to an infinite loop.
> Suggestion (untested):
>       
>       int ans = getchar();
>       for (int c = ans; c != '\n' && c != EOF;)
>               c = getchar();
>       if (ans != 'Y' && ans != 'y')
>               return 0;

I think this approach can still be a bit problematic, and we should skip
spaces at the beginning, and detect trailing characters (not tested):

        while (isspace(ch = getchar()))
                ;
        ans = ch;
        while (isspace(ch = getchar()) && ch != '\n')
                ;
        if (ch != '\n' && toupper(ans) != 'Y')
                return 0;

Kind Regards,
Roberto Vargas

Reply via email to