This is a bit of a long post so bear with me. The gist of what I am trying to do is get some user input at the beginning of a loop, do some things depending on that input and possibly repeat. Simple enough right? Here is what I have.
void displayMain(Album albums[], int num_albums){
int break_condition = 1;
int user_choice;
//keep looping until we decide to quit
while (break_condition){
user_choice = displayMenu();
switch(user_choice){
case 0 :
return; //does this need to be return 0 or can it just be return (or is it the same thing?)
case 1 :
displayAlbums(albums, num_albums);
break;
case 2 :
printf("display tracks\n");
break;
default :
printf("Invalid choice!\n\n");
break;
}
}
return;
}
The function displayMenu() basically grabs some user input and returns an int.
int displayMenu(void){
printf("Display Menu\n1. Display all albums\n2. Display the tracks in an album\n0. Return to the mainmenu\nPlease enter the choice:\n");
//grab and return the choice
int choice;
scanf("%d", &choice);
return choice;
}
So when I finally run all of this code, if I enter a "bad" choice, ie: one that takes the default path in the switch statement, the code enters an infinite loop. Repeating the contents
Display Menu
1. Display all albums
2. Display the tracks in an album
0. Return to the main menu
Please enter the choice:
Invalid choice!
I am pretty much a newb at C, so I believe I know why this is. user_choice is not ever being reset, even though the function displayMenu is clearly being called (because of the infinite loop output)
Why isn't displayMenu asking the user for input anymore after one instance of "bad" input is entered? Is it because it isn't an integer? When I tried debugging and printing out user_choice, it was some large number, so likely a register or something like that.
Please help, as I said, I suck at C, so an experienced C programmer could probably lend a hand :)