Greetings,
I have written a simple menu program, and am trying to get my error checking to work. What I am having problems with is if I enter a letter rather then a number. If I enter a number that is not an option, everything works great, but if I enter a letter, like F of H I get in some loop, and can not get out. I did search the boards and found a few things, but nothing that seems to clear up my issue, unless I want to rewrite my code. Obviously if I am way off, and need to rewrite it I can, but I am hoping someone might have an idea how I can correct my issue without redoing the whole thing.
Here is my code:
// Menu Driven Program
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
int getMenu (void);
void function1 (void);
void fnction2 (void);
void function3 (void);
int main (void)
{
int done = 0;
int choose;
while (!done)
{
choose = getMenu ();
if (choose == 4)
done = 1;
else
switch (choose)
{
case 1: functionq ();
break;
case 2: functions2 ();
break;
case 3: function3();
break;
}
printf("\n\n");
}
return 0;
}
// --------------------getMenu--------------------
int getMenu (void)
{
int choice;
do
{
printf("\t**************************************** \n");
printf("\t* Menu *\n");
printf("\t* *\n");
printf("\t* 1. Option 1 *\n");
printf("\t* 2. Option 2 *\n");
printf("\t* 3. Option 3 *\n");
printf("\t* 4. Exit Program *\n");
printf("\t* *\n");
printf("\t**************************************** \n\n");
printf("Please select a menu optionh (1-4) and press <retrun>: ");
scanf("%d", &choice);
if (choice <1 || choice >4)
printf("\aSorry, but this is not a valid option. Please select again.\n\n");
} while (choice <1 || choice >4);
return choice;
}
All ideas and help is appreciated.
Thanks,
DD