Reference no: EM132200826
Below is a function that has a user enter (y)es or (n)o. And give an error and make them re-enter if anything other than 'y', 'Y', 'n', or 'N' is entered. It gives the error correctly if a single character is entered, but will print out multiple error messages at once if 3 or more characters are typed in and I don't understand why. My code is posted below.
int yes(void)
{
char entry = '';
char newLine = '';
int result = 0;
while ((entry != 'y' && entry != 'Y' && entry != 'n' && entry != 'N') || newLine != ' ')
scanf("%c%c", &entry, &newLine);
if ((entry != 'y' && entry != 'Y' && entry != 'n' && entry != 'N') || newLine != ' ')
{
printf("*** INVALID ENTRY *** <Only (Y)es or (N)o are acceptable>: ")
}
else if (entry == 'y' || entry == 'Y')
{
result = 1;
}
}
return result;
}
Below is an example of the output that I'm receiving:
Do you want to enter a middle initial(s)? (y or n): f
*** INVALID ENTRY *** <Only (Y)es or (N)o are acceptable>: f
*** INVALID ENTRY *** <Only (Y)es or (N)o are acceptable>: f
*** INVALID ENTRY *** <Only (Y)es or (N)o are acceptable>: fff
*** INVALID ENTRY *** <Only (Y)es or (N)o are acceptable>:
*** INVALID ENTRY *** <Only (Y)es or (N)o are acceptable>: fffff
*** INVALID ENTRY *** <Only (Y)es or (N)o are acceptable>:
*** INVALID ENTRY *** <Only (Y)es or (N)o are acceptable>:
*** INVALID ENTRY *** <Only (Y)es or (N)o are acceptable>:
Depending on how many characters are entered seems to determine how many extra errors are printed.