So I'm trying making a simple menu to a program that only accepts the options 1,2 and 3 and I want to control the user input to prevent errors like when the user inputs char instead of int.
Right now it controls some cases like if the option is less than 1 or greater than 3, if the option is a char it also does not affect but when the user inputs something like " 02" or "2a" it runs option 2 but should invalidate that option.
Also if there´s more cases that I'm missing I would like to know them and how to overcome them.
#include <stdio.h>
#include <stdlib.h>
void empty_stdin(void);
int main() {
int option;
int rtn;
do {
printf("\n--\nOptions:\n1.Option 1\n2.Option 2\n3.Option 3\n--\n\nPlease chose option (1/2/3) to continue: ");
rtn = scanf("%d", &option);
if (rtn == 0 || option < 1 || option > 3) {
printf("-Invalid Option-\n");
empty_stdin();
} else {
empty_stdin();
switch (option) {
case 1:
printf("Option 1");
break;
case 2:
printf("Option 2");
break;
case 3:
printf("Option 3");
exit(0);
default:
printf("\n-Invalid Option-\n");
}
}
} while (option != 3);
return 0;
}
void empty_stdin(void) {
int c = getchar();
while (c != '\n' && c != EOF)
c = getchar();
}
Example Input/Output
--
Options:
1.Option 1
2.Option 2
3.Option 3
--
Please chose option (1/2/3) to continue: 1
Option 1
--
Options:
1.Option 1
2.Option 2
3.Option 3
--
Please chose option (1/2/3) to continue: 12
-Invalid Option-
--
Options:
1.Option 1
2.Option 2
3.Option 3
--
Please chose option (1/2/3) to continue: char
-Invalid Option-
--
Options:
1.Option 1
2.Option 2
3.Option 3
--
Please chose option (1/2/3) to continue: 02
Option 2
--
Options:
1.Option 1
2.Option 2
3.Option 3
--
Please chose option (1/2/3) to continue: 2a
Option 2
--
Options:
1.Option 1
2.Option 2
3.Option 3
--
Please chose option (1/2/3) to continue: 02a
Option 2
--
Options:
1.Option 1
2.Option 2
3.Option 3
--
Expected Input/Output
--
Options:
1.Option 1
2.Option 2
3.Option 3
--
Please chose option (1/2/3) to continue: 1
Option 1
--
Options:
1.Option 1
2.Option 2
3.Option 3
--
Please chose option (1/2/3) to continue: 12
-Invalid Option-
--
Options:
1.Option 1
2.Option 2
3.Option 3
--
Please chose option (1/2/3) to continue: char
-Invalid Option-
--
Options:
1.Option 1
2.Option 2
3.Option 3
--
Please chose option (1/2/3) to continue: 02
-Invalid Option-
--
Options:
1.Option 1
2.Option 2
3.Option 3
--
Please chose option (1/2/3) to continue: 2a
-Invalid Option-
--
Options:
1.Option 1
2.Option 2
3.Option 3
--
Please chose option (1/2/3) to continue: 02a
-Invalid Option-
--
Options:
1.Option 1
2.Option 2
3.Option 3
--