im trying to write a simple function to prompt for a y/n input for a given question.
#include <stdio.h>
int getChoice(char*);
int main()
{
int a = getChoice("yes?");
printf("%d\n",a);
return 0;
}
int getChoice(char* msg) {
char choice;
printf("%s\n", msg);
scanf("%c", &choice);
while (choice != 'y' && choice != 'n') {
printf("Please enter only y or n.\n");
scanf("%c", &choice);
}
return choice == 'y';
}
If I enter y or n it runs as expected, but if I enter another letter, it will duplicate the line saying "Please enter only y or n.". I really cant workout where my logic goes wrong.