I have a recursive function that uses printf()
and scanf()
to deal with user input. If the user accidentally presses a wrong key (not 'y'), it loops back. However, if the user enters multiple characters, the function asks the user for input multiple times at once. The printf()
lines are repeated as many times as characters there are in the input.
Why does having excessive characters in the terminal buffer result in this type of behavior rather than the whole thing crashing or the terminal just ignoring the following characters?
int testDetectorWhite(){
char userConfirmation;
printf("Please place a WHITE disk directly below the detector\n");
printf("Press 'y' when ready:\n");
scanf("%c", &userConfirmation);
printf("\n");
if(userConfirmation != 'y'){
testDetectorWhite();
}
//more code
}
I tried rewriting the code so that the recursive if() is substituted for a while() loop, like the following:
while(userConfirmation != 'y') {
printf("Please place a WHITE disk directly below the detector\n");
printf("Press 'y' when ready:\n");
scanf("%c", &userConfirmation);
}
However, I still got the exact same bug.
I tried solving the issue by making userConfirmation
a string, which solved the issue of multiple printf()
functions running but resulted in more issues that are outside the scope of my question.