Task 156 - Luhn Algorithm - Using C Lang

Back to General discussions forum

hypernine     2020-03-01 04:51:40

I have a specific problem. I keep getting -38 bewteen the "corrected" credit card numbers. The only thing i can think of after about 12 hours of trying to solve this problem is that this is the code for the return(enter)key. This -38 is causing every number after the first corrected card number to be incorrect.

I've gone over this with a debugger so many times and i dont understand where a -38 is coming from. (yes i'm new to programming).

any help would be worshipped.

I would be more than happy to show my code if someone want me to post it.

Rodion (admin)     2020-03-01 06:10:31
User avatar

Hi Friend!

Verily, it's rather specific problem, though it doesn't look like related to any of the codes for return key (13 or 10) I know. So probably it would be better to upload your code, yes.

Generally your unsuccessful solutions are visible at this page linked from your profile, but it seems you haven't yet submitted this problem in C, right?

hypernine     2020-03-03 01:41:10

Hello sir!

yes sir your are correct. I haven't submitted the problem in C yet because i dont have a working solution on my end. At the moment i can feed one CC number in at a time and i can get a correct answer(unless its a switch number problem and the number keeps switching numbers waaaay out of bounds and i get a segfault because i didnt code anything in to stop the continual switching of *next num). But when I try and do a for loop in C with a scanf for 16 numbers for some reason it will give me a d@m! -38 on the first input of next number(or the 17th char).

AND thank you for the info. I was not aware that the site posted the code of an unsuccessful code solution. I will go ahead and put the code in.

hypernine     2020-03-03 06:54:41

ok i just ran my solution through my debugger again watching it slowly and its placing the value 10'' in the 1st position of the array.

OH and i just noticed in your first comment about 13 or 10! ok so... well poop. Im not sure i know how to fix this anyway. guess i'll look input and output in the K&R C lang book.

hypernine     2020-03-03 07:14:17

i got some progress tho! i just entered an extra scanf in between the number of cards to test and the first card and can now at least pass the example problem with 4 numbers. I seem to get a seg fault everytime i try and do an actual set of forty something numbers.

I DID notice tho, that some of the numbers that i test can switch every set of numbers and not find the correct solution.. so?.. i'm not sure how to proceed now.

Quandray     2020-03-03 19:31:53
User avatar

scanf("%c", &arr[i]); will read a single character, including whitespace, i.e. end of line.
To ignore whitespace you could try scanf(" %c", &arr[i]); note the space, but for this problem, I don't understand why you are reading single characters. I suggest you use scanf("%s",arr); after ensuring that arr is long enough.

hypernine     2020-03-04 22:12:09

well i was reading single characters so i could have my code know where the question mark was located. Sound like I'm going about the problem all wrong.

Rodion (admin)     2020-03-05 05:18:19
User avatar

No, not "all wrong" - it surely can be solved in both ways. What Quandray suggests is somewhat easier, because scanf reading lines will remove non-visible characters.

guess i'll look input and output in the K&R C lang book.

Well, as I remember, at least in the first part they rely mainly on getc / putc to read "per character". There is also gets to read whole lines... Anyway you are right, you should use this book heavily - pure C language is not the simplest (though not the hardest) and sometimes you need to figure out how to deal with data etc... :)

Please login and solve 5 problems to be able to post at forum