0

Hy CS50

I working on the Credit program for pset1 right now, but somehow it seems like my output is still a bit messed up... and i just don´t get where the issue is! Here the output from check50, and my code further below.

:) credit.c exists.
:) credit.c compiles.
:) identifies 378282246310005 as AMEX
:) identifies 371449635398431 as AMEX
:) identifies 5555555555554444 as MASTERCARD
:( 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
:) rejects a non-numeric input of "foo"
:) rejects a non-numeric input of ""

Here the Code :

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

int numberLength(long long ccNumber)
{
      int counter = 0;

      while (ccNumber > 0)
      {
          counter++;
          ccNumber = ccNumber / 10;
      }

      return counter;
}

bool lengthCheck(int counting)
{
    if (counting == 13)
    {
        return true;
    }
    else if (counting == 15)
    {
        return true;
    }
    else if (counting == 16)
    {
        return true;
    }
    else
    {
        return false;
    }

}

int main()
{
    long long ccNumber = 0;
    int counter = 0;
    int lengthChecked = 0;


    printf("Please enter your Credit Card Number: ");
    ccNumber = get_long_long();
    counter = numberLength(ccNumber);
    lengthChecked = lengthCheck(counter);

    int ccArray[counter]; 

    int iterator = counter - 1;

    while (iterator >= 0)
    {
        int digit = ccNumber % 10;
        ccArray[iterator] = digit;
        ccNumber = ccNumber / 10;
        iterator--;
    }

    int firstSum = 0;
    int secondSum = 0;

    int doubleCheck = 0;

    for (int i = 0; i < counter; i++)
    {
        if (i % 2 == 0)
        {
            secondSum = secondSum + ccArray[i]; 
        }

        if (i % 2 == 1)
        {
            doubleCheck = ccArray[i] * 2;
            if (doubleCheck >= 10)
            {
                firstSum = firstSum + (doubleCheck % 10);
                firstSum = firstSum + (doubleCheck / 10);
            }
            else
            {
                firstSum += doubleCheck;
            }
        }
    }

    int totalChecksum = firstSum + secondSum;

    if (totalChecksum % 10 == 0 && lengthChecked == 1)
    {
        if (ccArray[0] == 3)
        {
            if (ccArray[1] == 4 || ccArray[1] == 7)
            printf("AMEX\n");
        }
        else if (ccArray[0] == 4)
        {
            printf("VISA\n");
        }
        else if (ccArray[0] == 5)
        {
            if (ccArray[1] == 1 || ccArray[1] == 2 || ccArray[1] == 3 || ccArray[1] == 4 || ccArray[1] == 5)
            {
                printf("MASTERCARD\n");
            }
        }
    }
    else
    {
        printf("INVALID\n");
    }

    return 0;

}
1

The doubling of digits starts with the second to last, not with the second. There's a difference for even number of digits. It's probably a coincidence if some of the numbers pass (or they are specifically crafted that way to tell this problem apart from others).

Also, I don't like that you check the length independently from the type. For example, a seemingly valid AMEX with 13 or 16 digits should still be rejected.

BTW: What happens if checksum is valid, length might be appropriate, but the card number for example starts with a 1 (which is none of the three types)?

1
  • Ah, i get it now... if i start with the second number, the last digit will fall off on cc numbers of odd length. I also removed the function for the length-check and used it directly on the if else checks for the type of Card. You helped me a lot here, thank you very much :) Sep 15 '17 at 16:41

You must log in to answer this question.

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