So I finished credit.c today and was trying out different numbers from the paypal website the course provided and got some unexpected results and don't know what to do with it. When I type in valid credit card numbers, it correctly outputs the corresponding name (AMEX, VISA, MASTERCARD) and if I input invalid ones it also outputs INVALID, all as intended. Here comes my problem though: If I input valid cc numbers from THIS link which don't correspond to the cases I defined for AMEX, VISA and MASTERCARD, it does not output invalid but nothing at all! I tried 2221000000000009 (MasterCard), 6011000990139424 (Discover) and others. Can someone help me fix this?
Here's my code:
#include <stdio.h>
#include <cs50.h>
#include <math.h>
long long get_ccn(string prompt);
int main(void)
{
int n1, n2, s1, s2;
long long cc_n = get_ccn("Enter CC: ");
long cc_n1 = cc_n;
s1 = 0;
s2 = 0;
while (cc_n > 0)
{
//Take last digit
n1 = cc_n % 10;
//Add last digit to first sum
s1 += n1;
//Go to next digit
cc_n -= n1;
cc_n /= 10;
//Take digit for second sum
n2 = cc_n % 10;
//Take digit * 2, if it's 10 or more, add each digit to second sum
if ((n2 * 2) > 9)
{
int n2_t2 = n2 * 2;
s2 += n2_t2 % 10;
s2 += (n2_t2 - (n2_t2 % 10)) / 10;
}
else
{
s2 += (n2 * 2);
}
//Go to next digit
cc_n -= n2;
cc_n /= 10;
}
//Check what kind of CC it is and if it's valid
if ((s1 + s2) % 10 == 0)
{
bool amex, mastercard, visa;
amex = (cc_n1 >= 340000000000000 && cc_n1 < 350000000000000) || (cc_n1 >= 370000000000000 && cc_n1 < 380000000000000);
mastercard = (cc_n1 >= 5100000000000000 && cc_n1 < 5600000000000000);
visa = (cc_n1 >= 4000000000000 && cc_n1 < 5000000000000) || (cc_n1 >= 4000000000000000 && cc_n1 < 5000000000000000);
if (amex)
{
printf("AMEX\n");
}
else if (mastercard)
{
printf("MASTERCARD\n");
}
else if (visa)
{
printf("VISA\n");
}
}
else
{
printf("INVALID\n");
}
}
long long get_ccn(string prompt)
{
long long n;
do
{
n = get_long("%s", prompt);
}
while (n < 0);
return n;
}