#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void option1()
{ printf("You have selected option 1\n");}
void option2()
{ printf("You have selected option 2\n");}
void option3()
{ printf("You have selected option 3\n");}
void option4()
{ printf("You have selected option 4\n");}
int get_option()
{
printf("Menu:\n\n");
printf("1) One\n");
printf("2) Two\n");
printf("3) Three\n");
printf("4) Four\n");
printf("5) Five\n");
printf("Please select option 1-5: ");
int opt;
scanf("%d", &opt);
return opt;
}
int main()
{
int i = get_option();
while (i < 1 || i > 5)
{
printf("You have entered an invalid input character. Please try again.\n\n");
i = get_option();
}
if (i == 1)
option1();
elseif (i == 2)
option2();
elseif (i == 3)
option3();
elseif (i == 4)
option4();
elseif (i == 5)
exit(1);
system("pause");
return 0;
}
Basically, I'm just presenting the user with a menu of options, and prompting them to enter one. If they enter a valid number, then the function corresponding to that number is executed (at this point, the functions are pretty worthless). If they don't enter a valid number or character, they are supposed to be notified of that, presented the menu again, and prompted to enter another number. At this point, mine works okay unless a non-numeric value is entered, in which case it loops on indefinitely.
Any suggestions on how to get it to respond "normally" to a character or string input (i.e. so that it just informs the user that they entered invalid input, and allows them to try again) rather than going crazy??? Thanks.