Thread: help needed to an easy code of validating a credit card number

1. help needed to an easy code of validating a credit card number

Hello everyone , i'm new to programming, and get often stuck when making some programming exercises, but I usually managed to just google the problem. This time I'm stuck with this assignment from CS50 Harvard course called "credit". The code seems to work fine until it needs to check which type of card it is (Visa American Express or Master Card ) . The problem seems to be somewhere in the last part where it checks the conditions for different types of cards (i wrote a comment in red ) .
I know the code isn't very "reader friendly" , but i was trying my best Code:
int main(void) {

int sum1=0,sum2=0,sum3,arr,i, a, i1, total_digits=0;
long long num,num2;

/* asking user for a valid card number */
printf("enter a card number:");
do
{
scanf("%lld",&num);
}
while (num<0);
num2=num;
printf("%lld \n",num2);

/*counting the number of digits in this card number */
while (num2!=0)
{
num2=num2/10;
total_digits++;
}
/*storing those digits in the array*/
for (a=total_digits-1;a>=0;a--)
{
arr[a]=num%10;
num=num/10;
printf ("%i \n",arr[a]);
}

/*algorithm for checking if the card number is valid*/
for (i=total_digits-2;i>=0;i-=2)
{
arr[i]=arr[i]*2;
printf("array[ %i ]= %i",i,arr[i]);

if (arr[i]>9)
{
i1=i;
arr[i1]=arr[i]%10;
sum1+= (1+arr[i1]);
}
else sum1+=arr[i];
printf("sum1 is %i\n",sum1);
}
for (i=total_digits-1;i>=0;i-=2)
{
sum2+=arr[i];
}

printf("sum2 is %d\n",sum2);
sum3=sum2+sum1;
printf("sum3 is %i\n",sum3);

/* From here the code doesn't seem to work, it just prints that something went wrong*/

/*if the result of the algorithm above ends with zero then the card is valid and can be checked for the type */
if ( sum3%10==0 )
{
if (total_digits==16 && arr[total_digits-16]==5 && (arr[total_digits-15]==1 || arr[total_digits-15]==2 ||arr[total_digits-15]==3 ||arr[total_digits-15]==4||arr[total_digits-15]==5))

{
printf("MasterCARD");
}

else if (total_digits==15 && arr[total_digits-15]==3 && (arr[total_digits-14]==4 || arr[total_digits-14]==7))
{
printf("this is AmericanExpress");
}

else if ((total_digits==13 || total_digits==16 ) && (arr[total_digits-13]==4 || arr[total_digits-16]==4))
{
printf("this is visa");
}

else printf("something went wrong \n"); }

else printf("the card is invalid");
return 0;
} 2. I would think it is a little more complicated than what you have presented. 3. Originally Posted by rstanley I would think it is a little more complicated than what you have presented.

Oh yes , this seems to be way more complicated than what i'm doing i was using the exercise guidelines from this c course from harvard, they made it probably more easy to understand in order to write the code. They are only giving small algorithm for checking the card validity and what digits those different types of card start. I can't post the link as one needs to be logged in but i'll copy the exercise description , in case it would help A credit (or debit) card, of course, is a plastic card with which you can pay for goods and services. Printed on that card is a number thats also stored in a database somewhere, so that when your card is used to buy something, the creditor knows whom to bill. There are a lot of people with credit cards in this world, so those numbers are pretty long: American Express uses 15-digit numbers, MasterCard uses 16-digit numbers, and Visa uses 13- and 16-digit numbers. And those are decimal numbers (0 through 9), not binary, which means, for instance, that American Express could print as many as 10^15 = 1,000,000,000,000,000 unique cards! (Thats, um, a quadrillion.)
Actually, thats a bit of an exaggeration, because credit card numbers actually have some structure to them. All American Express numbers start with 34 or 37; most MasterCard numbers start with 51, 52, 53, 54, or 55; and all Visa numbers start with 4. But credit card numbers also have a checksum built into them, a mathematical relationship between at least one number and others. That checksum enables computers (or humans who like math) to detect typos (e.g., transpositions), if not fraudulent numbers, without having to query a database, which can be slow. Of course, a dishonest mathematician could certainly craft a fake number that nonetheless respects the mathematical constraint, so a database lookup is still necessary for more rigorous checks.
Luhns Algorithm

So whats the secret formula? Well, most cards use an algorithm invented by Hans Peter Luhn of IBM. According to Luhns algorithm, you can determine if a credit card number is (syntactically) valid as follows:

1. Multiply every other digit by 2, starting with the numbers second-to-last digit, and then add those products digits together.
2. Add the sum to the sum of the digits that werent multiplied by 2.
3. If the totals last digit is 0 (or, put more formally, if the total modulo 10 is congruent to 0), the number is valid!

Thats kind of confusing, so lets try an example with Davids Visa: 4003600000000014.

1. For the sake of discussion, lets first underline every other digit, starting with the numbers second-to-last digit:
4003600000000014
Okay, lets multiply each of the underlined digits by 2:
12 + 02 + 02 + 02 + 02 + 62 + 02 + 42
That gives us:
2 + 0 + 0 + 0 + 0 + 12 + 0 + 8
Now lets add those products digits (i.e., not the products themselves) together:
2 + 0 + 0 + 0 + 0 + 1 + 2 + 0 + 8 = 13
2. Now lets add that sum (13) to the sum of the digits that werent multiplied by 2 (starting from the end):
13 + 4 + 0 + 0 + 0 + 0 + 0 + 3 + 0 = 20
3. Yup, the last digit in that sum (20) is a 0, so Davids card is legit!

So, validating credit card numbers isnt hard, but it does get a bit tedious by hand. Lets write a program. Popular pages Recent additions Tags for this Thread

card, code, problem, total_digits==16, work 