Reputation: 637
This is the code
#include <stdio.h>
int main()
{
int num1 = 0, num2 = 0, num3 = 0, num4 = 0;
do
{
printf("choose 4 numbers, you dont need space\n");
scanf("%1d%1d%1d%1d", &num1, &num2, &num3, &num4);
if(!num1 || !num2 || !num3 || !num4)
{
getchar();
}
}
while (num1 != num2 || num1 != num3 || num1 != num4 ||
num2 != num3 || num2 != num4 || num3 != num4);
}
I don't know what to do with that.
If the input is, for example. */*-
then
the output is:
choose 4 numbers, you dont need space
choose 4 numbers, you dont need space
choose 4 numbers, you dont need space
choose 4 numbers, you dont need space
Upvotes: 0
Views: 54
Reputation: 6224
Your code behaves as you describe because the scanf
function does not touch the reading buffer when the convertion cannot take place. Calling the function in loop will try to read the next characters as %d
.
Entering a non-numeric sequence will never stop your code as expected.
For this reason, you should check the return value of scanf
in order to detect bad inputs.
Upvotes: 1