Credit Card Verifier (Luhn’s algorithm)


#1

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.


#2
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


#3

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


#4

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


#5

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


#6

Replacing 20 with 21 solves the problem here.

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

Can you both confirm?


#7

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


#8

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.


#9

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