0

I've been trying to put together a program that can recognise credit card numbers from different companies- I've been building it piecemeal, and I've managed to get almost every section to work. All the code seems to work up until the final section where I try to make sure that the input is between certain numbers to decide which company a card belongs to. The error I receive is on the line if (f == 0) && (number > 339999999999999) && (number < 380000000000000), apparently it expects an identifier for "number" - I thought the fact that I had declared it at the very top would circumvent this, but apparently not.

long long number;

int main(void)
{
    long long i = 0;
    long long b;
    long long m = 10;
    long long n = 1;
    number = get_long_long("Number?\n");
    do
    {
        long long a = number % m;
        b = number - a;
        long long c = b % (m * 10);
        long long d = c / m;
        long long e = d *2;
        if (e < 9)
        {
            i = i + e;
        }
        else
        {
            i = i + (e - 10 + 1);
        }
        
        {
            m = m * 100;
        }
    }
    while (b > 0);
    do
    {
        long long a = number % n;
        b = number - a;
        long long c = b % (n * 10);
        long long d = c / n;
        long long e = d *2;
        if (e < 9)
        {
            i = i + e;
        }
        else
        {
            i = i + (e - 10 + 1);
        }
        
        {
            n = n * 100;
        }
    }
    while (b > 0);
    int f = i % 10;
    if (f == 0) && (number > 339999999999999) && (number < 380000000000000)
    {
        printf("AMEX\n");
    }
    else
    if (f == 0) && (number > 1099999999999999) && (number < 5600000000000000)
    {
        printf("Mastercard\n");
    }
    else
    if (f == 0) && ((number > 3999999999999999) && (number < 5000000000000000) || (number > 3999999999999) && (number < 5000000000000))
    {
        printf("Visa\n");
    }
    else
    {
    printf("invalid\n");
    }
}
BlueKhakis
  • 293
  • 1
  • 8
  • 4
    You are missing brackets around the whole `if` condition `((f == 0) && (number > 339999999999999) && (number < 380000000000000))` – kaylum Dec 01 '20 at 00:10
  • 1
    You'll save yourself a lot of trouble by reading the number as a string. – user3386109 Dec 01 '20 at 00:17
  • @kaylum I modified this as you suggested- it now points to the OR operator, with the error: "expected expression". – BlueKhakis Dec 01 '20 at 00:17
  • @user3386109 do you mean I should replace all instances of "long long" with "string"? – BlueKhakis Dec 01 '20 at 00:18
  • 1
    As far as I know, credit cards numbers aren't really numbers, they're strings of digits. And they're not necessarily 16 in length either, so a numeric range test is not the correct way to do this. The first 6 or 8 digits denote the IIN and that tells you the issuer. Amex, for example, is 34xxxx or 37xxxx. Specifically, Amex does not begin 35 or 36, which your algorithm will incorrectly identify as Amex. Also, read: https://stackoverflow.com/questions/72768/how-do-you-detect-credit-card-type-based-on-number – jarmod Dec 01 '20 at 00:21
  • That's the first step. Search for `[c] Luhn` for examples. – user3386109 Dec 01 '20 at 00:24
  • @jarmod Thanks, I'll try to make some adjustments. – BlueKhakis Dec 01 '20 at 00:25
  • @BlueKhakis Can't tell you what the problem is without seeing the **exact** modification you did. – kaylum Dec 01 '20 at 00:30
  • @kaylum Ah I just put a bracket in the wrong place, fixed, thanks. – BlueKhakis Dec 01 '20 at 00:33

0 Answers0