Credit Card Verifier (Luhn’s algorithm)

Hi,
I think that this challenge if you choose C the suggested code is broken; in particolar fgets(card,20,stdin); It does not seem to work.

int n;
scanf("%d", &n); fgetc(stdin);
for (int i = 0; i < n; i++) {
    char card[20];
    fgets(card, 20, stdin);
    fprintf(stderr, card);
}
//4556 7375 8689 9855

All seams to be good to me

Yes, but it jump one card. If you try the first test it should print 2 cards for example but doesn’t

You are right :confused: and i do not know how to resolve this

i fix it using gets instead of fgets but it should never be used

Replacing 20 with 21 solves the problem here.

char card[21];
fgets(card, 21, stdin);

Can you both confirm?

Yes it does, but isn’t it weird since the length of the string is 19 ?

Nope, you need one extra char for the \n (newline) plus one extra char for the \0 that indicates the end of the string (in C there is no string object, you use large enough char[MaxSize] instead, hence the length of a string is not explicitly specified and you need one way to mark the end, which is done by a \0 char).

Edit: I fixed the default code generator for this puzzle.

1 Like

Yes it didn’t worked for me. Try to generate real credit cards online for free.

Ya i also have checked this. It is not printing the required cards.

@saraparker1121 Which language are you using?