I want to use scanf to get two integers separated by a comma from the user.
So the scanf is in a loop that checks if the user really gave two integers or just some random strings, and if he did gave strings the program prints an error message and loop again. (if the user gave two integers the loop breaks)
so this is my code:
unsigned int x, y;
printf("Please enter your coordinates: ");
while (true){
fflush(stdin);
if (scanf("%d, %d", &x, &y) == 2){
printf("%d, %d\n", x, y);
break;
} else {
printf("Not a valid format! please try again: "); // error message
}
}
But when I run this the program just prints the error message endlessly.
From what I understand the program should wait again for another user input in the scanf (which is also why I did fflush, so it will clean the stdin buffer)
I really don't know what's going on, so any answer will help!
Thank you
Edit: I know that there are better ways to do it that scanf, but unfortunately I have to do it this way