1. ## newbie Q. about do-while loop

a warm hello to everybody

my problem with my first program is that it is looping twice then ends. I would like to loop till user enters a number between 1 and 5.
So no symbols or letters or numbers out of this range.

Code:
```    int numberOfPlayer;
int numberOfPlayerTester = 0;

printf("Hey to the drinkerman game!\n\n");
printf("how many people want to get drunk (max 5 players):\n");
scanf(" %d", &numberOfPlayer);

do{
if(numberOfPlayer != 5 && numberOfPlayer != 4 && numberOfPlayer != 3 && numberOfPlayer != 2 && numberOfPlayer != 1)
{
printf("\nSorry, but you have to choose a number between 1 and 5.\nSo how many are you?\n");
scanf(" %d,", &numberOfPlayer);
}else
{
numberOfPlayerTester = 1;
}

}while(numberOfPlayerTester = 0);```

2. Take a closer look at line #21. What do you think it does? Before you answer, let me remind you that "a = 0" is totally different from "a == 0".

3. perfect greaper, the numbers are doing fine.
now if user enters a symbol or a letter it prints endless lines of line13 (without stopping)

has it to do with the fact that "numberOfPlayer" is an int?
But how can I solve it?

4. You see, the only input after the loop starts is done inside that if statement, that's why it's behaving that way. You should move the scanf out of that if-else block( but still inside the loop ).

Also, you have a stray comma in line #13, inside the string. You should remove that.

5. Code:
```    int numberOfPlayer;
int numberOfPlayerTester = 0;

do{
scanf(" %d,", &numberOfPlayer);
if(numberOfPlayer != 5 && numberOfPlayer != 4 && numberOfPlayer != 3 && numberOfPlayer != 2 && numberOfPlayer != 1)
{
printf("\nSorry but you have to choose a number between 1 and 5.\nSo how many are you?\n");
}else
{
numberOfPlayerTester = 1;
}

}while(numberOfPlayerTester == 0);```
still not going... with letters or symbols still endless lines

6. It works fine when I compile and run it. No endless lines or anything.

7. Originally Posted by kingkoks
now if user enters a symbol or a letter it prints endless lines of line13 (without stopping)
If "scanf()" encounters a type it does not expect (such as the user entering a character when an integer is expect), "scanf()" fails, and what caused it to fail remains on the input buffer.

So in your case, when you enter a character, "scanf()" tries to read it and fails. Code execution continues, the loop is repeated, "scanf()" tries to read the same thing and fails again, and so on. Infinite loop.

You can check whether "scanf()" successfully read input by checking its return value. "scanf()" returns the number of items successfully read, so if you're trying to read a single integer, "scanf()" will return 1 if successful.

If "scanf()" was not successful, you need to clear away whatever is on the input buffer. One way is to use a "while" loop as shown here: FAQ > Flush the input buffer - Cprogramming.com

Alternatively, you could use "fgets()" to read an entire line from the user, and "sscanf()" to retrieve the integer. Failure handling is a bit cleaner with this approach.