Nicolas Diken
Nicolas Diken

Reputation: 13

Error with this code? [C]

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

Answers (2)

Dilip Kumar
Dilip Kumar

Reputation: 1746

  1. m is not initialized;
  2. 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

Ayushi Jha
Ayushi Jha

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

Related Questions