0

I wrote this code and I can "make credit". However, when I tested it with numbers, it doesn't work.

Please see my code below.

#include <cs50.h>
#include <stdio.h>
#include <math.h>

int main(void)
{
    int number = get_int("Number: ");
    int sumofln;
    int sumofeon;
    
    for (int n = 1; n < 17; n = n + 2)
    {
        int i = pow(10, n);
        int eon = number % i; //eon = every other number
        
        sumofeon = 0;
        sumofeon = sumofeon + eon; //sumofeon = sum of every other number
    }
    for (int n = 0; n < 17; n++)
    {
        int j = pow(10, n);
        int ln = number % j; //ln = left numbers
         
        sumofln = 0; //sumofln = sum of left numbers
        sumofln = sumofln + ln;
    }
    
    int total;
    total = sumofln + sumofeon;
    
    if (total % 10 == 0)
    {
        if ((number % 1000000000000000) == 4) //VISA - 16
        {
            printf("VISA\n");
        }
        else if ((number % 1000000000000000) == 5) // MASTERCARD
        {
            if ((number % 100000000000000) < 6)
            {
                printf("MASTERCAD\n");
            }
            else
            {
                printf("INVALD\n");
            }
        }
        else if (number % 1000000000000000 == 0 && number % 100000000000000 == 3) //AMEX
        {
            if (number % 10000000000000 == 4 || number % 10000000000000 == 7)
            {
                printf("AMEX\n");
            }
            else
            {
                printf("INVALID\n");
            }
        }
        else if ((number % 1000000000000000 + number % 100000000000000 + number % 10000000000000) == 0) //VISA - 13
        {
            printf("VISA\n");
        }
        else
        {
            printf("INVALID\n");
        }
    }
    else
    {
        printf("INVALID\n");
    }
}

1
  • You are suffering from a very old disease in programming called the "It cannot be I" syndrome. A programmer convinced themselves that they have done absolutely nothing wrong, yet the program still isn't working. Computers do exactly what they're told but not necessarily what they are intended to do. If a program doesn't do what is expected, there is always a bug or a logic error. It's that simple. Once you embrace that concept, debugging will get a lot easier. Remember, there's a difference between "There is no bug" and "I can't see the bug."
    – Cliff B
    Commented Nov 14, 2020 at 23:14

1 Answer 1

1

One fundamental issue you'll face is that a credit card number is too long for an int; try using get_long() instead.

I would recommend starting over with your implementation of Luhn's algorithm. Something that may help: you can get the last digit of a number by using modulo 10 (% 10) and drop the last digit with integer division by 10 (/ 10).

1
  • Thank you! I finally figure it out after spending 6 hours on this problem. I started this course 4 days ago. It's not easy for me to have the computer thinking so far but I won't give up!
    – ys-cao
    Commented Nov 16, 2020 at 13:38

You must log in to answer this question.

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