Character Input Validation
Hello it's me, again...
I am having a problem with a character input validation function.
Code:
char check_letter(void)
{
char input, ch;
while (scanf("%c", &input) != 1 || input != 'A' && input != 'P')
{
while ((ch = getchar()) != '\n')
;
printf("Enter correct value: ");
}
return input;
}
Basically this function checks if the user input 1) a valid letter character, and 2) the user enters either A or P, and this is case-sensitive.
When I test this function, I enter in A or P as it asks "Enter a letter (A or P)". When I enter A for example, I get the error message "Enter correct value: "
Now when I enter in A again or even P the 2nd try, the function accepts the letter as valid input and continues on with the rest of the program.
Again, I am very new to coding, I have looked at various books and researched on the web but have not found anything useful to tell me how to properly create a function like this.
I asked a few days ago about integer input validation functions and I got that working, but it appears letters act a bit different?
I would appreciate any constructive help :) Thanks!