Stuck on credit card validation problem | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

21st Feb 2022, 3:54 AM
JRS
JRS - avatar
4 Answers
+ 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)?
21st Feb 2022, 4:04 AM
HungryTradie
HungryTradie - avatar
+ 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.
21st Feb 2022, 4:14 AM
JRS
JRS - avatar
+ 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
21st Feb 2022, 4:20 AM
HungryTradie
HungryTradie - avatar
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 .
21st Feb 2022, 5:13 AM
JRS
JRS - avatar