I am new to C. I am writing this do while loop but when I accidentally enter a decimal/double to the input, it will enter a infinite loop. Would you please correct me? Thank you!
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main(void)
{
int items;
do {
printf("Input item quantity: ");
scanf("%d", &items);
if (items < 1 || items > 10)
printf("ERROR: It must between 1 - 10.\n");
} while (items < 1 || items > 10);
return 0;
}
If I try to input 300.5, the output will become a loop
```
Input item quantity: ERROR: It must between 1 - 10.
Input item quantity: ERROR: It must between 1 - 10.
Input item quantity: ERROR: It must between 1 - 10.
```
scanfis not designed for robust user input with error recovery. There are ways to flush the offending input if the user makes a mistake, but if this is just a toy program for learning, you can cheat and tell the user (you) to not make mistakes. If you want to do robust input, it's far easier to use something other thanscanf.