ccs50

Why is there no output when working with certain card numbers? cs50 credit problem


The program asks the user for a credit card number and then reports (via printf) whether it is a valid American Express, MasterCard, or Visa card number, using luhns algorithm and the requirements for each card. When testing the code, some card numbers just have no output.The source code and results of the test are below:

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

bool validation(long number) {
    long i = 10;
    long j = 1;
    int totalMultiplied = 0;
    int totalNotMultiplied = 0;

    while (number / i != 0){
        totalMultiplied += (((number / i) % 10) * 2) % 10 + (((number / i) % 10) * 2) / 10;
        i *= 100;
    }
    while (number / j != 0) {
        totalNotMultiplied += (number / j) % 10;
        j *= 100;
    }

    return ((totalMultiplied + totalNotMultiplied) % 10 == 0);
}

int main(void) {
    long creditCardNo;
    do {
        creditCardNo = get_long("Enter credit card number: ");
    }
    while (creditCardNo < 0);

    int count = 0;
    long startNumbers = creditCardNo;
    long n = creditCardNo;

    do {
        n /= 10;
        count++;
    }
    while (n != 0);

    while (startNumbers >= 100) {
        startNumbers /= 10;
    }



    if (validation(creditCardNo)) {
        if (count == 15 && ( startNumbers == 34 || startNumbers == 37)) {
            printf("AMEX\n");
        }
        else if (count == 16 && ( startNumbers == 51 || startNumbers == 52 || startNumbers == 53 || startNumbers == 54 || startNumbers == 55)) {
            printf("MASTERCARD\n");
        }
        else if ((count == 13 || count == 16) && ( startNumbers / 10 == 4)) {
            printf("VISA\n");
        }
    }else {
        printf("INVALID\n");

    }


}



The results of the test:

Results for cs50/problems/2024/x/credit generated by check50 v3.3.11
:) credit.c exists
:) credit.c compiles
:) identifies 378282246310005 as AMEX
:) identifies 371449635398431 as AMEX
:) identifies 5555555555554444 as MASTERCARD
:) identifies 5105105105105100 as MASTERCARD
:) identifies 4111111111111111 as VISA
:) identifies 4012888888881881 as VISA
:) identifies 4222222222222 as VISA
:) identifies 1234567890 as INVALID (invalid length, checksum, identifying digits)
:( identifies 369421438430814 as INVALID (invalid identifying digits)
    expected "INVALID\n", not ""
:( identifies 4062901840 as INVALID (invalid length)
    expected "INVALID\n", not ""
:( identifies 5673598276138003 as INVALID (invalid identifying digits)
    expected "INVALID\n", not ""
:) identifies 4111111111111113 as INVALID (invalid checksum)
:) identifies 4222222222223 as INVALID (invalid checksum)
:( identifies 3400000000000620 as INVALID (AMEX identifying digits, VISA/Mastercard length)
    expected "INVALID\n", not ""
:( identifies 430000000000000 as INVALID (VISA identifying digits, AMEX length)
    expected "INVALID\n", not ""

As you can see the output for certain card numbers is just "" an empty string and I'm not sure why.


Solution

  • There is a hole in your logic in this block:

    if (validation(creditCardNo)) {
        if (count == 15 && ( startNumbers == 34 || startNumbers == 37)) {
            printf("AMEX\n");
        }
        else if (count == 16 && ( startNumbers == 51 || startNumbers == 52 || startNumbers == 53 || startNumbers == 54 || startNumbers == 55)) {
            printf("MASTERCARD\n");
        }
        else if ((count == 13 || count == 16) && ( startNumbers / 10 == 4)) {
            printf("VISA\n");
        }
    }else {
        printf("INVALID\n");
    
    }
    

    It is logically possible for your validation() to return a true, and then fail to meet the other conditions inside that branch.

    You have all the source code, throw in some break points and step through some of your tests.