0

For Problem Set 1, I have the code as written below

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

int card_length(long cNo);
void luhn_algo(long lacno, int lacnolen);
void finalPrint(int cardDet);

int main(void)
{
    //Get user input of card number
    long cardNo = get_long("Number: ");
    int ct = card_length(cardNo);

    //Check if card no is length 13, 15 or 16
    if (ct == 13 || ct == 15 || ct == 16)
    {
        luhn_algo(cardNo, ct);
    }
    else
    {
        // Invalid due to unacceptable length
        printf("INVALID\n");
    }
    return 0;
}

//Function to get card length
int card_length(long cNo)
{
    int count = 0;
    while (cNo > 0)
    {
        count += 1;
        cNo /= 10;
    }
    return count;
}

//lacno is Luhn_Algo_Card_Number, lacnolen is Luhn_Algo_Card_No_LENgth
void luhn_algo(long lacno, int lacnolen)
{
    //Initialize array of name cardElem with elements eq to card length 
    int cardElem[lacnolen];
    // Initialize integer arrayElem equal to card length
    int arrayElem = lacnolen;
    //counter is the element that is the 2nd from right
    int counter = lacnolen - 2;
    // ct is the card length
    int ct = lacnolen;
    //Initialize variable for the sum at the end of luhn alg
    int sum;

    //Populate array values with card numbers starting from the ones position
    while (arrayElem > 0)
    {
        cardElem[arrayElem - 1] = lacno % 10;
        lacno /= 10;
        arrayElem--;
    }

    //Save a variable called firstValue which is the first value of the card, or the 0th value of the array
    int firstVal = cardElem[0];

    //Implementation of the luhn algorithm
    for (int i = counter; i >= 0; i = i - 2)
    {
        cardElem[i] *= 2;
        if (cardElem[i] > 9)
        {
            int w = cardElem[i] % 10;
            cardElem[i] /= 10;
            int x = cardElem[i];
            cardElem[i] = w + x;
        }
    }

    //Adding up the elements in the overwritten value of the array
    for (int j = 0; j < ct; j++)
    {
        sum = sum + cardElem[j];
    }

    if (sum % 10 == 0)
    {
       finalPrint(firstVal);
    }
    else
    {
        printf("INVALID\n");
    }
}

void finalPrint(int cardDet)
{
    if (cardDet == 3)
    {
        printf("AMEX\n");
    }
    else if (cardDet == 5)
    {
        printf("MASTERCARD\n");
    }
    else if (cardDet == 4)
    {
        printf("VISA\n");
    }
}

I run make credit && ./credit and try selected card numbers and it provides expected results. However when I try check50 cs50/problems/2020/x/credit this is the result

:) credit.c exists
:) credit.c compiles
:( identifies 378282246310005 as AMEX
    expected "AMEX\n", not "INVALID\n"
:( identifies 371449635398431 as AMEX
    expected "AMEX\n", not "INVALID\n"
:( identifies 5555555555554444 as MASTERCARD
    expected "MASTERCARD\n", not "INVALID\n"
:( identifies 5105105105105100 as MASTERCARD
    expected "MASTERCARD\n", not "INVALID\n"
:( identifies 4111111111111111 as VISA
    expected "VISA\n", not "INVALID\n"
:( identifies 4012888888881881 as VISA
    expected "VISA\n", not "INVALID\n"
:) identifies 1234567890 as INVALID
:) identifies 369421438430814 as INVALID
:) identifies 4062901840 as INVALID
:) identifies 5673598276138003 as INVALID
:) identifies 4111111111111113 as INVALID

I have tried to change some of the printf outputs and determined that the INVALID that the program outputs in check50 is the one in luhn_algo, but I haven't managed to debug further from there.

1 Answer 1

0

When I run your code, I get the same results as check50. My guess is that the code that you have posted above matches what you have submitted to check50, but the code that you're testing locally is different.

Is it possible that you are working with two different versions of the source code? I'm thinking that you have been editing and testing one version - the one you compiled and have gotten successful results, but have another version in another directory that you ran with check50. Or, maybe you have two versions of the code with slightly different file names?

You must log in to answer this question.

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