I am trying to develop the main menu for a project in school and I cannot figure out how to best scan get my menu option as a character and have it select properly from the switch statement. It is off by one, as in i will enter G for the first time, it loops without changing or outputting anything, I will enter a different letter, and it outputs the results I had needed for my previous input. This loop continues, and does not exit until I enter the exit condition followed by another input.
I have tried restructuring the do/while statement, as well as convert all of the letters to a single case for a simpler switch structure using toupper().
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
char menuOption = '\0';
printf("*****************************************\n");
printf(" Welcome to the Races! \n");
printf(" Select from the menu to continue \n");
printf("*****************************************\n");
printf("\n");
do{
printf("************** MENU **************\n");
printf("* [G]amble *\n");
printf("* [B]anking *\n");
printf("* [R]ace Results *\n");
printf("* [L]eave the Track *\n");
printf("*****************************************\n");
scanf("%c\n", &menuOption);
switch(menuOption){
case 'G':
case 'g':
printf("Case G\n");
break;
//Insert Function to jump to gambling options
case 'B':
case 'b':
printf("Case B\n");
break;
//Insert Functions to show tabulated banking
case 'R':
case 'r':
printf("Case R\n");
break;
//Insert Functions to show results and wagers from gambling
case 'l':
case 'L':
break;
default:
printf("%c is not a valid menu option. Please enter a valid menu option.\n", menuOption);
return 0;
}
printf("You have entered %c.\n", menuOption);
}while (menuOption != 'l' || menuOption != 'L');
printf("Thank you for gambling with us!");
// Proper exit message
return 0;
}