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

  1. #1
    Registered User
    Join Date
    Feb 2019

    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

    int main(void) {
    int sum1=0,sum2=0,sum3,arr[20],i, a, i1, total_digits=0;
    long long num,num2;
    /* asking user for a valid card number */
    printf("enter a card number:");
    while (num<0); 
    printf("%lld \n",num2);
    /*counting the number of digits in this card number */
    while (num2!=0) 
    /*storing those digits in the array*/
    for (a=total_digits-1;a>=0;a--)
    printf ("%i \n",arr[a]);
    /*algorithm for checking if the card number is valid*/
    for (i=total_digits-2;i>=0;i-=2) 
    printf("array[ %i ]= %i",i,arr[i]);
    if (arr[i]>9) 
    sum1+= (1+arr[i1]); 
    else sum1+=arr[i];
    printf("sum1 is %i\n",sum1);
    for (i=total_digits-1;i>=0;i-=2) 
    printf("sum2 is %d\n",sum2);
    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)) 
    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;
    Last edited by anetac; 02-15-2019 at 04:08 PM.

  2. #2
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    New York, NY
    I would think it is a little more complicated than what you have presented.

    Check this site for more information.

  3. #3
    Registered User
    Join Date
    Feb 2019
    Quote Originally Posted by rstanley View Post
    I would think it is a little more complicated than what you have presented.

    Check this site for more information.
    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:
      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 subscribe to a feed

Similar Threads

  1. Need help with a credit card program
    By ChackoTaco in forum C Programming
    Replies: 1
    Last Post: 10-03-2016, 09:21 PM
  2. C: Credit Card Number Validator - Luhn Algorithm
    By Lioneyexp in forum C Programming
    Replies: 1
    Last Post: 02-07-2012, 07:06 PM
  3. [Help] Program : How to validate credit card number
    By kingofdcp in forum C Programming
    Replies: 13
    Last Post: 10-31-2009, 12:58 AM
  4. I need help with this last project! Credit Card Program!
    By ClearSights in forum C++ Programming
    Replies: 18
    Last Post: 10-27-2008, 10:08 AM

Tags for this Thread