I'm just starting to learn C, and basically trying to program something very simple to start. The program does work fine (if used as intended), but my issue is that if the user types something while the program is simply displaying text to the screen (which is after an option on the menu is pressed, but before it goes back to the main menu), that typing of characters at that point and hitting ENTER causes the program to loop over and over to one of the menu option results (as if an option was selected over and over again from the menu screen).

Ideally, it would be best if I can prevent any user input during the printf/sleep statements, but otherwise I would like to find a way so that input is ignored.

I stripped down the code to a very basic example, but here is what I have.

I actually re-wrote this code later and instead of using SWITCH/CASE statements, I changed it to IF / ELSE IF / ELSE, but encountered the exact same problem.

I'm using sleep statements because I'm clearing the screen and want enough time for the user to read the message before it clears again and goes back to the menu.

What happens though is if during that time (when the message is being displayed), if the user types something and hits ENTER, instead of going back to the menu it basically loops over and over again on a particular message (and not even the 'default' message, just the one that already displayed. Screen clears, it displays message, screen clears, it displays message, etc.. over and over)

One thing I experimented with was adding a 2nd variable for non-integer input (what I call "garbage"), since the menu should only accept numbers anyway, but that didn't fix it. What I tried was adding a garbage variable hoping to collect non-internet input so it wouldn't get processed:

int choice;
char garbage;

and then:

scanf("%d",&choice);
scanf("%d",&garbage);

That didn't help.

When I rewrote this program to use IF / ELSE IF / ELSE instead, I pretty much hard coded all the menu items, like this:

else if(choice == 2)
else if(choice == 3)
else

where the 'else' was the catch-all if anything other than 2 or 3 were selected, but the same behavior occurs if someone types in text in a screen that they are just supposed to read the message on.

Any ideas to fix this? (something a beginner would understand, I'm sure there are complex solutions to this but that would be over my head right now most likely).