0

I had work on the credit for 3 hours, and I had reach to most of the function - and I checked that the number received from: Luhn (a) last digit(c) and digit count (cc) are right by using 4003600000000014, which a = 20, c = 0 and cc = 16

However, for some reasons that I keep getting INVALID as answer, can anyone found out why and let me know? Thanks a lot QvQ

# include <stdio.h>

include <cs50.h>

int main (void){ long n = 0; do{ n = get_long ("Please Enter Your Credit Card Number:"); } while (n < 0);

int n1 = n % 100/10*2;
int n2 = n % 10000/1000*2;
int n3 = n % 1000000/100000*2;
int n4 = n % 100000000/10000000*2;
int n5 = n % 10000000000/1000000000*2;
int n6 = n % 1000000000000/100000000000*2;
int n7 = n % 100000000000000/100000000000000*2;
int n8 = n % 10000000000000000/1000000000000000*2;

 // Make sure each number is 1 digit

int f1 = n1 % 100/10;
int f1a = n1 % 10;
int f2 = n2 % 100/10;
int f2a = n2 % 10;
int f3 = n3 % 100/10;
int f3a = n3 % 10;
int f4 = n4 % 100/10;
int f4a = n4 % 10;
int f5 = n5 % 100/10;
int f5a = n5 % 10;
int f6 = n6 % 100/10;
int f6a = n6 % 10;
int f7 = n7 % 100/10;
int f7a = n7 % 10;
int f8 = n8 % 100/10;
int f8a = n8 % 10;

int t1 = n % 10;
int t2 = n % 1000/100;
int t3 = n % 100000/10000;
int t4 = n % 10000000/1000000;
int t5 = n % 1000000000/1000000000;
int t6 = n % 100000000000/100000000000;
int t7 = n % 10000000000000/1000000000000;
int t8 = n % 1000000000000000/100000000000000;

int q = f1+f1a+f2+f2a+f3+f3a+f4+f4a+f5+f5a+f6+f6a+f7+f7a+f8+f8a;
int a = q + t1 + t2 + t3 + t4 + t5 + t6 + t7 + t8;
int c = a % 10;
int cc = 0;//count digit
int nn = n;//count rest of n
do{
     nn = nn/10;
     cc ++;
}
while (nn > 0);
//checksum 1
if (c == 0){
        if (cc == 15){
            int t = n % 100000000000000/1000000000000;
            //checksum AMEx
            if (t == 34 || t == 37){
                printf("AMEX\n");
            }   
            else{
                printf("INVALID\n");   
            }
            }
    //checksum Visa
    else if  (cc == 13){
        int v1 = n % 1000000000000/100000000000;
            if (v1 == 4){
                printf("VISA\n");
                }
            else{
                printf("INVALID\n");
            }
        }
    // checksum visa and master
        else if (cc == 16){
            int x = n % 1000000000000000/100000000000000;
            int y = n % 1000000000000000/10000000000000;
                if (y == 4){
                    printf("VISA\n");
                    }
                else{
                    if (x >= 51 && x <=55){
                    printf("MASTERCARD\n");
                    }
                else {
                    printf("INVALID\n");
            }
        }
        }
        }
        else {
            printf("INVALID\n");
        }
        

}

1 Answer 1

1

ok so when you are doing this -> " int t = n % 100000000000000/1000000000000;" The '/' sign is dividing your 1000000000.. by 1000000... I think you meant to use '/' sign as a symbol for 'or'. The resultant x or y are not single digits.

Besides you are making this code unnecessarily long and complicated. i would suggest you to go through the walkthrough again. If you have any confusion regarding this feel free to ask. I or someone else in the forum would be happy to help you. Good luck!

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .