If user enters a character instead of a number, I want to give him another option to try once more but below code prints out "Invalid. Pls enter a number." forever if user enters a character instead of a number. Why doesn't it wait for the user to enter again? (scanf part is for that I assume)
#include <stdio.h>
long get_long(void);
int main(void) {
long start;
printf("Enter a number: ");
start = get_long();
return 0;
}
long get_long(void)
{
long num = 0;
while (scanf("%ld", &num) != 1)
{
printf("Invalid. Pls enter a number.");
}
return num;
}