I've written a function yes(); that is used to confirm if the user wants to exit the program. If 'Y' or 'y' is entered it works fine and the user exits and if 'N' or 'n' is entered it also works fine with the user returning to the menu to select another option however, if anything aside from those characters are entered an error message displays informing the user that only (Y)es or (N)o are acceptable however, the message does not provide the user another opportunity to input a valid option, but instead proceeds to display the menu. Here's my code:
void GroceryInventorySystem(void) {
int menuSelection;
int exitSelection = 0;
welcome();
printf("\n");
while (exitSelection == 0) {
menuSelection = menu();
switch (menuSelection)
{
case 1:
printf("List Items under construction!\n");
pause();
break;
case 2:
printf("Search Items under construction!\n");
pause();
break;
case 3:
printf("Checkout Item under construction!\n");
pause();
break;
case 4:
printf("Stock Item under construction!\n");
pause();
break;
case 5:
printf("Add/Update Item under construction!\n");
pause();
break;
case 6:
printf("Delete Item under construction!\n");
pause();
break;
case 7:
printf("Search by name under construction!\n");
pause();
break;
default:
printf("Exit the program? (Y)es/(N)o: ");
exitSelection = yes();
break;
}
}
}
int yes(void) {
char YN;
int exit = 0;
scanf("%c", &YN);
if (YN == 'Y' || YN == 'y') {
exit = 1;
}
else if (YN == 'N' || YN == 'n') {
exit = 0;
}
else {
printf("Only (Y)es or (N)o are acceptable: \n");
}
return exit;
}