I’m having problems with this snippet of code:
while(scanf("%d",&numOfPlayers)!=1){
printf("Please enter the right number of players");
}
My purpose is to read a number from the user, and keep asking as long as the input isn’t an int. When I run this snippet and input ‘r’ for example, I get stuck in an infinite loop. What might be the problem, and how can I improve it?
If
scanf()can’t read the input, it actually doesn’t read it, so it reads the same input over and over again in your example. You can discard the invalid input like this: