I am having trouble with this snippet of code:
while (scanf ( "% D", and amp; numOfPlayers)! = 1) {printf ("Please enter the correct number of players"); }
My aim is to read a number from the user, and keep asking for as long as there is no input input. When I run this snippet and input 'R' for example, I get stuck in an infinite loop. What can be the problem, and how can I improve it?
If scanf ()
can not read input, Not reading, so it reads the same input repeatedly in your example. You can discard invalid input like this:
while (scanf ("% d", and numOfPlayers)! = 1) {scanf ("% s"); Printf ("Please enter the correct number of players \ n"); }
Comments
Post a Comment