#include <stdio.h>
int main() {
while (1){
int a;
printf("Enter a random integer: ");
if (scanf("%d", &a) == 1){
printf("Yeah! You entered an integer number!\n");
}
else {
printf("Error: That is not an integer. Try again!\n");
}
}
return 0;
}
If I run the code, it works fine when I entered an integer. But if I enter a string, I expect an error message but instead, I got an infinite loop of message. How could I modify my code such that if the user enter a string it will just print an error message and ask again.