0

My solution for "credit" compiles just fine with make, but when using check50, I get

:( credit.c compiles.
    expected exit code 0, not 1

My code is as follows:

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

int validateCCNumber(long long num)
{
    // Set up holding variables
    int digit1 = 0;
    int digit2 = 0;
    int counter = 0;
    int sumOfEvens = 0;
    int sumOfDoubleOdds = 0;

    while (num > 0)
    {
        digit2 = digit1;
        digit1 = num % 10;

        if (counter % 2 == 0)
        {
            sumOfEvens += digit1;
        }
        else
        {
            sumOfDoubleOdds += (2 * digit1 / 10) + (2 * digit1 % 10);
        }

        num /= 10;
        counter++;
    }

    if ((sumOfEvens + sumOfDoubleOdds) % 10 == 0)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

int main(void)
{
    long long num = get_long_long("Number: ");

    if (num - 100000000000000 < 0) // Less than 15 digits
    {
        printf("INVALID\n");
    }
    else
    {
        if (num - 1000000000000000 < 0) // Exactly 15 digits, so check if it's Amex
        {
            if (num / 10000000000000 != 34 && num / 10000000000000 != 37) // First digits aren't valid Amex digits
            {
                printf("INVALID\n");
            }
            else
            {
                // Check that number is valid
                if (validateCCNumber(num))
                {
                    printf("AMEX\n");
                }
                else
                {
                    printf("INVALID\n");
                }
            }
        }
        else if (num - 10000000000000000 >= 0) // More than 16 digits
        {
            printf("INVALID\n");
        }
        else // Exactly 16 digits
        {
            if (num / 1000000000000000 == 4) // Visa
            {
                // Check validity
                if (validateCCNumber(num))
                {
                    printf("VISA\n");
                }
                else
                {
                    printf("INVALID\n");
                }
            }
            else if (num / 100000000000000 == 51 || num / 100000000000000 == 52 || num / 100000000000000 == 53
                     || num / 100000000000000 == 54 || num / 100000000000000 == 55) // Mastercard
            {
                // Check validity
                if (validateCCNumber(num))
                {
                    printf("MASTERCARD\n");
                }
                else
                {
                    printf("INVALID\n");
                }
            }
            else // Not Mastercard or Visa
            {
                printf("INVALID\n");
            }
        }
    }

    return 0;
    }
1

Might be you have a slightly different version of cs50 library.

On 64 bit Linux (but not on Windows), long long is same as long, both 8 bytes, so somebody decided to deprecate get_long_long (see get_long_long in the reference).

I guess you need to use get_long, not get_long_long, for the same result on this platform.

BTW, the task description includes

and Visa uses 13- and 16-digit numbers

so you should not discard all numbers with less than 15 digits. Maybe count the digits and use that number to make that code more readable. Could also get first two digits that way.

1
  • You were right - changing long long to long got check50 to accept it. The problem description says to use get_long_long ("Do not assume that the user’s input will fit in an int! Best to use get_long_long from CS50’s library to get users' input. (Why?)". That should probably be changed. Thanks! – Tim Jan 22 '19 at 18:05

You must log in to answer this question.

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