I'm used to use the functions getche() or getch() for accepting choices for menus, and I tried to experiment using scanf(). The program example below works fine if I enter an integer, but when I entered any other keys (i.e. A-z) that's not a number, the program crashes. I tried to use isdigit() and isalpha() to detect the error and correct it--but to no avail.
Anyone who can teach me how to detect a non-integer input to an integer-type variable using scanf()?Code:#include <stdio.h> #include <conio.h> int main() { int choice; do { clrscr(); printf("Binary and Decimal numbers\n\n"); printf("What do you want to do?\n1. Convert Decimal to Binary\n"); printf("2. Convert Binary to Decimal\n3. or Exit this program\n"); printf("Enter number of choice: "); scanf("%d", &choice); switch(choice) { case 1: printf("1"); break; case 2: printf("2"); break; case 3: break; default: printf("Invalid choice!"); choice = 0; // trying to replace a non-integer input but won't work. break; } getch(); } while(choice != 3); return 0; }