0

Finished my credit problem, whit the code below, but when I check it with check50 everything fails despite the output on the terminal being correct when I check manually.

Code:

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

long get_credit_card_nb(void)
{
    long    nb;

    nb = 0;
    while (nb < 1)
        nb = get_long("Number: ");
    return (nb);
}

int get_cc_len(long cc)
{
    int len;

    len = 0;
    while (cc > 0)
    {
        cc /= 10;
        len++;
    }
    return (len);
}

int check_cc_len(int len)
{
    if (len != 13 && len != 15 && len != 16)
    {
        printf("INVALID\n");
        return (0);
    }
}

int check_sum(long cc)
{
    int d1;
    int d2;
    int mod1;
    int mod2;
    int sum1;
    int sum2;

    sum1 = 0;
    sum2 = 0;
    while (cc > 0)
    {
        // Remove last digit and add to sum1
        mod1 = cc % 10;
        cc /= 10;
        sum1 += mod1;

        // Remove second last digit
        mod2 =  cc % 10;
        cc /= 10;

        // Double second last digit and add digits to sum2
        mod2 *=  2;
        d1 = mod2 % 10;
        d2 = mod2 / 10;
        sum2 += d1 + d2;
    }
    return (sum1 + sum2);
}

int get_start_digits(long cc)
{
    while (cc > 100)
        cc /= 10;
    return (cc);
}

int main(void)
{
    int     len;
    int     total;
    int     start_digits;
    long    credit_card;

    credit_card = get_credit_card_nb();
    len = get_cc_len(credit_card);
    if (!check_cc_len(len))
        return (0);
    else
    {
        total = check_sum(credit_card);
        if (total % 10 != 0)
        {
            printf("INVALID\n");
            return (0);
        }
        start_digits = get_start_digits(credit_card);
        if ((start_digits / 10 == 5) &&
             (0 < start_digits % 10 && start_digits % 10 < 6))
            printf("MASTERCARD\n");
        else if ((start_digits / 10 == 3) &&
                 (start_digits % 10 == 4 || start_digits % 10 == 7))
            printf("AMEX\n");
        else if (start_digits / 10 == 4)
            printf("VISA\n");
        else
            printf("INVALID\n");
    }
    return (0);
}

check50

:) credit.c exists
:) credit.c compiles
:( identifies 378282246310005 as AMEX
    expected "AMEX\n", not ""
:( identifies 371449635398431 as AMEX
    expected "AMEX\n", not ""
:( identifies 5555555555554444 as MASTERCARD
    expected "MASTERCARD\n", not ""
:( identifies 5105105105105100 as MASTERCARD
    expected "MASTERCARD\n", not ""
:( identifies 4111111111111111 as VISA
    expected "VISA\n", not ""
:( identifies 4012888888881881 as VISA
    expected "VISA\n", not ""
:( identifies 4222222222222 as VISA
    expected "VISA\n", not ""
:) identifies 1234567890 as INVALID
:( identifies 369421438430814 as INVALID
    expected "INVALID\n", not ""
:) identifies 4062901840 as INVALID
:( identifies 5673598276138003 as INVALID
    expected "INVALID\n", not ""
:( identifies 4111111111111113 as INVALID
    expected "INVALID\n", not ""
:( identifies 4222222222223 as INVALID
    expected "INVALID\n", not ""
To see the results in your browser go to https://submit.cs50.io/check50/180b26efe4f3302c8178cab39eb27048ed3d187b

Someone can point what is wrong withthe output?

1 Answer 1

0

Your code "works" as written, but it doesn't match the program specifications, so check50 fails many of the tests because the code is waiting for new input that check50 doesn't provide and it simply times out.

Your code will continuously request a new cc number until it gets a valid number. Any entry that contains a non-number causes the code to loop and request a new number. This doesn't match the spec. It should accept ANY input from the user and then validate. It should NOT ask for new input.

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

3
  • I could change the way I get the input, getting rid of the loop get_credit_card_nb(void) since get_long() treats it, but the ending result is the same. I really do not understand what I am missing here. I receive the input and validate its size, checksum, first digits... but at the end check50 always marks it as wrong.
    – harndt
    Nov 12, 2022 at 18:55
  • Check50 expects exact results, no stray printing of anything. I made the changes that I suggested and it passes everything. You say you "could" make the changes. Did you??? Sounds like you're suffering from a very old affliction among programmers, the "It cannot be I" syndrome. Programmers become so sure that the code is right that they refuse to accept that they could have made an error. The cure is to ALWAYS assume that if the code is not doing exactly what it should, that the programmer made an error. It's just that simple. BTW, it's an extremely simple fix.
    – Cliff B
    Nov 12, 2022 at 20:34
  • Oh, btw, you did remember to recompile, right?
    – Cliff B
    Nov 12, 2022 at 20:36

You must log in to answer this question.

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