I'm working on a project for school and my recursive menu() function isn't working properly. It's supposed to stop to wait for a scanf() input, but when called, runs all the way through itself then goes again, this time stopping. Here is the code. All the other functions referenced here work correctly. The variables also all work and are declared and everything. So are the arrays.
//FUNCTION FOR MENU
void menu() {
//DECLARE LOCAL VARIABLES
char allowedMenuInputs[8] = {'U', 'u', 'A', 'a', 'O', 'o', 'Q', 'q'};
//DISPLAY MENU
printf("MENU\n");
printf("u - Update stock price\n");
printf("a - Output stocks above a price\n");
printf("o - Output stock list\n");
printf("q - Quit\n\n");
//PROMPT USER
printf("Choose an option:\n");
//SCAN FOR INPUT
scanf("%c", &menuInput);
printf("\n"); //NEWLINE
//CHECK INPUT
if ((menuInput == allowedMenuInputs[0]) || (menuInput == allowedMenuInputs[1])) {
stockUpdate();
menu();
}
else if ((menuInput == allowedMenuInputs[2]) || (menuInput == allowedMenuInputs[3])) {
priceCheck();
menu();
}
else if ((menuInput == allowedMenuInputs[4]) || (menuInput == allowedMenuInputs[5])) {
displayStocks();
menu();
}
else if ((menuInput == allowedMenuInputs[6]) || (menuInput == allowedMenuInputs[7])) {
return 0;
}
else {
printf("Invalid option! Please try again!!\n");
menu();
}
}
int main(void) {
//WELCOME MESSAGE
printf("Welcome to the Stock Portfolio Program!\n\n");
//USER INPUT
for (i = 0; i < NUM_STOCKS; ++i) {
printf("Enter Stock ID number: \n");
scanf("%d", &(stockID[i]));
printf("Enter Stock Price $:\n");
scanf("%lf", &(stockPrice[i]));
printf("\n\n"); //NEWLINE
}
//OUTPUT STOCK LIST
displayStocks();
//OPEN MENU
menu();
return 0;
}
Here is the output
As you can see, the menu function runs through itself without stopping for input. All help appreciated!
I have no idea what to try and what causes this, as this has never happened to me before.