I have a scanf function that takes an integer 1,2, or 0. My default statement will return to a loop if there is not an input of these integers. If a character is entered, the default case still works, displays an error and returns to main. My question is if I should be using another while loop to check the scanf function for an integer, or if it is okay to keep the default statement which will return for any invalid input.
int main(int argc, char* argv[]) {
int value;
value = length_orWeight();
while (value != 0) {
value = length_orWeight();
}
return 0;
}
int length_orWeight(void) {
int choice;
printf("\nWhat would you like to convert?\n0.End Program 1.Lengths 2.Weights: ");
scanf("%d", &choice);
clear_keyboard_buffer();
switch (choice) {
case 1:
convert_lengths();
return 1;
case 2:
convert_weights();
return 2;
case 0:
return 0;
default:
printf("\nError: You must enter 0, 1, or 2.\n");
return 3;
}
}