I should've said from the beginning that the program would be a continuous loop, but I didn't think the loop would add such complications. My fault...
Code:
int main(int argc, char** argv) {
int exercises;
char str[50];
while (1) {
printf( "1. Option 1\n"
"2. Option 2\n"
"3. Option 3\n"
"4. Option 4\n"
"5. Quit\n"
"Choose a case: ");
while( scanf( "%d", &exercises ) != 1 ) {
char buf[ BUFSIZ ] = {0};
fgets( buf, BUFSIZ, stdin );
}
switch (exercises) {
case 1: printf("1\n"); break;
case 2: printf("2\n"); break;
case 3: printf("3\n"); break;
case 4: printf("4\n"); break;
case 5: exit(EXIT_SUCCESS); break;
default: printf("Invalid command.\n"); break;
}
}
}
I'm back to quzah's original suggestion. That bit of code does indeed read a number, and ignores everything else.
Since I'm using it, I should know exactly what it's doing. Can anyone explain it? Also, can I use sscanf() in this particular code to check if it's anything else other than a number?