Reputation: 13
I'm supposed to modify a code repdigit.c
so that the user can enter more than one number to be tested for a repeated digit. For some reason though all my compiler says is No repeated digit
, no matter what number I input.
int main(void)
{
bool digit_seen[10] = {false};
int digit;
long n, m;
while (1)
{
printf("Enter a number (0 to terminate): ");
scanf("%ld", &n);
if (n == 0)
break;
n = m;
while (m > 0)
{
digit = m % 10;
if (digit_seen[digit])
break;
digit_seen[digit] = true;
m /= 10;
}
if (m > 0)
printf("Repeated digit\n");
else
printf("No repeated digit\n");
}
return 0;
}
Upvotes: 0
Views: 135
Reputation: 1746
m
is not initialized;You are reading n
and copying m
to n
; m
might contain garbage value.
n=m;
Solution: Change n=m;
to m=n;
Upvotes: 3
Reputation: 4023
The problem is with n=m
. Since m
is not initialized, it will contain some garbage value. You probably wanted to have m=n
.
Upvotes: 5