+ 2
Stuck on credit card validation problem
Wondering if someone could help me. I thought Iâd solved the âcredit card validatorâ problem, but my solution fails for two of the locked cases. I canât for the life of me figure out what the problem is. Any suggestions would be appreciated. https://code.sololearn.com/c7M0qx84uCxz/?ref=app
4 RĂ©ponses
+ 1
G'day JRS Nice code!
What debugging have you tried?
Have you got some test values that will give you not valid, eg less than 16 digits, more than 16 digits, only a 0, no input at all ..etc?
Have you got some test values that are valid? I think it is something to do with your handling of two digit results from the *2 process, but I'm not 100%. Let us know?
{Edit: you did realise that reversing the order is just a wild goose chase.... didn't you?}
EDIT2: I wonder if when you convert to char if the array gets a \0 (or a NULL, or however c# does it)?
+ 1
HungryTradie - Honestly, no, I didnât even think about thatâŠbut yeah, I now see that itâs going to be the same result forward or backward because the even digits will still be the ones changed either way. I thought my problem might be that they threw in letters or something, which is why I added the TryParse, but that didnât change anything. I will try working on the *2 part, though. Thanks for the suggestion.
+ 1
Reversing the array would change which digit needs to be multiplied. Every second when reversed is same as odd numbers when forwards. Eg skip original 16, 14, 12, etc but multiply original 1, 3, 5, etc.
(Hmm, did I get that right?? I think so...)
{EDIT: we also have to remember that arrays/(and pointers in C) start at 0 so digit 1 is arr[0]}
https://code.sololearn.com/c4UEaRJ2D9Mm/?ref=app
0
Solved it! The problem was two-fold. First, something was awry with my âreversalâ of the digits. I never figured out what, but didnât need toâI just doubled the odd digits instead of even ones. The second problem was when converting the char array to int array, I was trying to use the wrong functionâConvert.ToInt32 vs Char.GetNumericValue. Once I switched that, it worked. Thanks for your help, HungryTradie .