0

When sending my code to Check50, all the Credit Card checks go through ok but one:

:( identifies 4062901840 as INVALID expected "INVALID\n", not "VISA\n"

But doing the steps: 1) Multiply every other digit by 2, starting with the number’s second-to-last digit, and then add those products' digits together.

Digits: 4 1 9 6 4 2x: 8 2 18 12 8 add: 8 2 9 3 8 = 30

2) Add the sum to the sum of the digits that weren’t multiplied by 2. Digits: 0 8 0 2 0 = 10 + 30 = 40

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! So, wouldn't the number be valid?

What am I missing? Code below.

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

int countdigits(long num)
{
// count the number of digits
int count = 0;
// zero would return 0 digits - adjust
if (num == 0)
{
    (count = 1);
}
else
{
    while (num)
    {
        count++;
        num = num / 10;
    }
}
return (count);
} //end function

long digitnumber(long num, int pos)
{
    // return the digit in the position pos
    int digit = 0;
    int tmp = 0;
    if (pos > (countdigits(num)))
    {
    printf("Error, position %i is larger than number length (%i)\n", pos, countdigits(num));
    exit(0);
}
else
{
    for (int i = countdigits(num); i > 0; i--)
    {
        tmp = num % 10;
        if (i == pos)
        {
            digit = tmp;
        }
        num = (num / 10);
        //printf("Digit: %i i: %i num: %li\n", digit, i, num);
    }
}
return digit;
}

//Begin Main Program
int main(void)
{

// receive the value
long s = -1;
while (s < 0)
{
    s = get_long_long("Number: ");
}

// Step 1:
// Multiply every other digit by 2, starting with the
// number’s second-to-last digit, and then add those products' digits together.

int st = 0;
st = countdigits(s);
//printf("digits: %i\n", st);
int p = 0;

// Check Issuers depending on 2 first numbers

char *issuer = "INVALID";
if (digitnumber(s, 1) == 4)
{
    issuer = "VISA";
}

p = (digitnumber(s, 1) * 10) + (digitnumber(s, 2));

if ((p == 34) || (p == 37))
{
    issuer = "AMEX";
}

if ((p >= 51) && (p <= 55))
{
    issuer = "MASTERCARD";
}

//printf("First 2 digits: %i\n", p);
//printf("Issuer: %s\n", issuer);

// Now let's determine if the number is valid

int j = countdigits(s) - 1;
float check = 0;
int l = 0;
while (j > 0)
{
    l = digitnumber(s, j) * 2;
    check = check + l;
    if (l > 9)
    {
        check = check + digitnumber(l, 1) + digitnumber(l, 2) - l;
    }
    j = j - 2;
}

//printf("Check = %f\n", check);

j = countdigits(s);
while (j > 0)
{
    check = check + (digitnumber(s, j));
    j = j - 2;
}

//printf("Check = %f\n", check);

l = digitnumber(check, countdigits(check));
if (l != 0)
{
    issuer = "INVALID";
}

//printf("last number = %i\n", l);
printf("%s\n", issuer);
}
2

I feel your frustration, but you're looking in the wrong place. The number may pass the checksum calculation, but that isn't the problem. The number is only 10 digits. It should be either 13 or 16 digits for Visa.

I don't believe that I saw any code that checks the validity of the number's length.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

| improve this answer | |
  • Thanks! That was exactly the issue. Not sure how I missed that in the instructions. – PXSOCS Jul 6 '18 at 10:04

You must log in to answer this question.

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