1

So regarding Pset 1 Credit, I'm having this issue with Check50

My code identifies the card, but there's some error when input given at shorter length: Pset 1 Error

My code are the following:

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

long long get_number(void);
int digit_count(long long number);

int main(void)
{
    long long number = get_number();
    int digit = digit_count(number); // digit count using number inputted as input
}

long long get_number(void)
{
    long number;
    do
    {
        number = get_long_long("input number: ");
        if (number < 1000000000000 || number > 9999999999999999)
        {
        printf("INVALID\n");
        }
    }
    while (number < 1000000000000 || number > 9999999999999999); // below 12 or above 16 digit is reprompted while say invalid
    return number;
}

// lohn algorithm
int digit_count(long long number)
{
    int digits[17];
    int i = 0;
    int total_digits = 0;
    long long original_number = number;
    while(number > 0)
        {
        digits[i] = number % 10; // take last digit store as digits[0], [1] etc
        // printf("Current number: %lld, digits[i]: %d\n", number, digits[i]);
        number = number/10; //update number
        i++;
        }
    int j;
    for (j = 0; j < i; j++) // 0 < 16
    {
        if (j % 2 != 0) // odd digit, multiple than 2 then add it
        {
        digits[j] = digits[j] * 2;
             // if digits[j] after multipled > 9 then split digit
            if (digits[j] > 9)
                {
                    digits[j] = (digits[j] % 10) + 1; // imagine 12 % 10 = 2, + 1 = 3
                }
        }
        else // odd digits no need to split
            {
                    digits[j] = digits[j]; // unchanged
            }
    total_digits = total_digits + digits[j]; // add total digits so far with digits[j]
    // printf("total digits is %i\n", total_digits); // already correct
        }
    int luhn_last_digit = total_digits % 10; // 60 % 10 == 0 already correct
    int front2_number = original_number / pow(10,i - 2);
    int front1_number = original_number / pow(10,i - 1);
    // printf("front number is %i\n", front2_number);
    if (luhn_last_digit != 0)
        {
            printf("INVALID\n");
        }
    else if (luhn_last_digit == 0 && i == 15 && (front2_number == 37 || front2_number == 34))
        {
            printf("AMEX\n");
        }
    else if (luhn_last_digit == 0 && i == 16 && (front2_number == 51 || front2_number == 52 || front2_number == 53 || front2_number == 54 || front2_number == 55))
        {
            printf("MASTERCARD\n");
        }
    else if (luhn_last_digit == 0 && (i == 13 || i == 16) && front1_number == 4)
        {
            printf("VISA\n");
        }
    else
    {
        printf("INVALID\n");
    }
    return luhn_last_digit;
}

1 Answer 1

2

From the spec:

your program’s last line of output [should] be AMEX\n or MASTERCARD\n or VISA\n or INVALID\n, nothing more, nothing less.

This program will print "something more" if the length is invalid (i.e. "input number:").

2
  • thanks, just resolve it by removing print invalid and replace it with return 0 :)
    – Blacov.
    Commented Jan 19 at 15:07
  • please accept this answer or provide your own answer and accept that if this answer is not sufficient.
    – UpAndAdam
    Commented Jan 22 at 15:48

You must log in to answer this question.

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