I have a program with a menu like this:
Code:
char c;
char option;
do{
printf("\n1.Insert number: \n");
printf("2. Option 2\n");
printf("3. Option 3\n");
printf("4. Option 4\n");
printf("5. Quit\n");
printf("Enter Option: ");
scanf("%c", &option);
getchar();
if(option=='1'){
printf("Insert number:");
while((c=getchar()) != '\n'){
//call function here
}
}
else if(option=='2'){
//do something
}
else if(option=='3'){
//do something
}
else if (option=='4'){
//do something
}
else if(option=='5'){
printf("The program exits now!\n");
break;
}
else {
printf ("Invalid menu choice - try again\n")
}
} while (option != '5');
I want to validate that the entered option is acceptable. The system will check that the user entered an integer numeric value between 1-5. If the input is incorrect then the system will respond “Invalid menu choice ” and present the user with the menu.
When I enter 1, or 2,3,4,5.... my program works fine. But when I enter 125, the program jumps to option 1.
I know that I am wrong at getchar() line since it only gets a character. but I don't know how to fix it.
Any helps would be appreciated.