0

My credit.c code keeps inserting huge numbers in my array for seemingly no apparent reason. For example the user enters 45231 for the credit card number, the beginning of the array is fine and displays 1621400000000000 as it should. But then the rest of the array displays 134003043232753649044001043326530132764004332576042042020 and I have no idea why.

I have tried commenting out much of the code that changes values in the array in order to isolate the problem but different random numbers keep appearing anyways. Anyone have any ideas on how to fix this?

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

int main (void)
{
    //variables
    int finnum;
    int d[32]; //credit card digits array
    long sum;
    long pow1 = 10;
    long pow2 = 10;

//get credit card number
long cardnum = get_long_long ("Enter your credit card number: ");

//divide inputed card number into single digits and multiply every other digit by 2
for (int i = 0; i < 16; i++)
{
    //last digit
    if (i == 0)
    {
     d[i] = cardnum % pow1;
    }
    //every other digit starting from second last digit
    else if (i == 1 || i == 3 || i == 5 || i == 7 || i == 9 || i == 11 || i == 13 || i == 15)
    {

     d[i] = ((cardnum % pow1) / pow2) * 2;
     pow2 = pow2 * 10;
    //split digits multiplied by 2 into single digits if double digit numbers
     if (d[i] > 9)
     {
         d[i+16] = d[i] % 10;
         d[i] = d[i] / 10;
     }

    }

    //all other numbers
    else
    {
     d[i] = (cardnum % pow1) / pow2;
     pow2 = pow2 * 10;

    }
    //powers of 10 for modulo
    pow1 = pow1 * 10;
}

//add digits in array
for (int s = 0; s < 32; s++)
{
    sum = sum + d[s];
}

//determine final digit of the sum of numbers
finnum = sum % 10;

//print sum of numbers and final digit of sum to check program logic
printf ("%li\n %i\n", sum, finnum);

//validate and print if card is correct or not
for (int t = 0; t < 32; t++)
{
    printf ("%i", d[t]);
}


if (finnum == 0 && d[14] == 3 && (d[13] == 4 || d[13] == 7))
{
    printf ("AMEX\n");
}

else if (finnum == 0 && (d[15] == 4 || d[12] == 4))
{
    printf ("VISA\n");
}

else if (finnum == 0 && d[15] == 5 && (d[14] == 1 || d[14] == 2 || d[14] == 3 || d[14] == 4 || d[14] == 5))
{
    printf ("MASTERCARD\n");
}

else
{
    printf ("INVALID\n");
}

}

0

I don't get your loop, especially pow1 and pow2, but using int d[32] = {0}; might help with some seemingly random numbers, as it would initialise the array on declaration. Variables on the stack are not initialised by default, but you write to d[i+16] only under certain circumstances, leaving them at arbitrary values in other cases.

Also, for a bit more useful debug output, put something between the numbers, like printf("%i ", d[t]);, so they are separated (in this case by a space). Or write one per line, maybe together with the index, like printf("%2i %i\n", t, d[t]);

BTW, 1004000000000004 is not a valid VISA card. You might want to count the digits.

You must log in to answer this question.

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