I first attempted the code myself, then tweaked it after watching Deliberate Think's video on Youtube. While my code can be compiled, all credit card number inputs return "INVALID" e.g. when using the testing AMEX number on PayPal (378282246310005), check50 says that: "identifies 378282246310005 as AMEX expected "AMEX\n", not "INVALID\n". I think there may be a problem under //implementing Luhn's algorithm.
Below is my code:
#include <stdio.h>
#include <cs50.h>
int find_length(long long ccn);
bool check_luhn(long long ccn);
bool check_validity(long long ccn);
void print_card_brand(long long ccn);
//prompt user for credit card number; if invalid prompts again
int main(void)
{
long long ccn;
do
{
ccn = get_long_long("Credit Card Number: ");
}
while (ccn < 0);
if (check_validity(ccn))
print_card_brand(ccn);
else
printf("INVALID\n");
}
//find the length of the credit card number
int find_length(long long ccn)
{
int length;
for (length = 0; ccn!= 0; ccn /=10);
length++;
return length;
}
//implementing Luhn's algorithm
bool check_luhn(long long ccn)
{
int sum = 0;
for (int i = 0; ccn != 0; i++, ccn /= 10)
{
if (i % 2 == 0)
sum += ccn % 10;
else
{
int digit = 2 * (ccn % 10);
sum += digit / 10 + digit % 10;
}
}
return (sum % 10) == 0;
}
//check length and Luhn's algorithm
bool check_validity(long long ccn)
{
int length = find_length(ccn);
return (length == 13 || length == 15 || length == 16) && check_luhn(ccn);
}
//print credit card brand
void print_card_brand(long long ccn)
{
if ( (ccn >= 34e13 && ccn < 35e13) || (ccn >= 37e13 && ccn < 38e13) )
printf("AMEX\n");
else if (ccn >= 51e14 && ccn < 56e14)
printf("MASTERCARD\n");
else if ( (ccn >= 4e12 && ccn < 5e12) || (ccn >= 4e15 && ccn < 5e15) )
printf("VISA\n");
else
printf("INVALID\n");
}
Thank you all in advance for your help, I've been stuck on this for so many days. Having never done coding before, I have no idea what went wrong.