Search code examples
printfscanfdo-whilelogical-or

printf executes multiple times only on certain inputs


Program executes the printf command multiple times when the input is "08" or "09" and not in some other similar number, say "03" or "07".

do
{
printf("Enter date:");
scanf("%i/%i/%i", &d, &m, &y);
}
while (d !=0 || m != 0 || y != 0);

This is the output (numbers are entered by user) -

Enter date:3/6/8
Enter date:3/6/08
Enter date:Enter date:04/05/06
Enter date:08/08/08
Enter date:Enter date:Enter date:Enter date:01/02/03
Enter date:04/05/06

Why is this happening and how can I fix it ?


Solution

  • Hasn't been answered yet so this wouldn't hurt.

    I'm not sure if it's right but as I read more about C today, my guess is it must be happening because scanf is reading the input as an octal number because it starts from zero. And the strange behaviour might be because there is no 08 and 09 in octal.