Hi everyone,

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.


Code:
#include <stdio.h>

void load_menu(void);

int main (int argc, char** argv)
{
	load_menu();
	return 0;
}
void load_menu(void)
  {
	int choice;
	do
	{
	system("clear");
	printf("\n\n");
 	printf("PICK AN OPTION\n\n"); sleep(3);
 	printf("1. Choice 1\n");
 	printf("2. Choice 2\n");
	printf("3. Choice 3\n");
 	printf("4. Exit\n");
	printf("\n");
 	scanf("%d",&choice);
 	switch (choice)
 	{
     	case 3: system("clear"); printf("\n\n"); printf("You selected 3, you win!\n"); printf("\n"); sleep(5);
          break;
     	case 4: system("clear"); printf("\n"); printf("GOODBYE!\n"); printf("\n"); sleep(3); 
          break;
	default: system("clear"); printf("\n\n"); printf("You picked something other than 3 or 4.\n\n"); sleep(5); 
          break;
 	} 
  } while (choice !=10) ;
  return;
}

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).


Thanks in advance for any advice.