I’m trying to code a program that identifies whether a card is amex visa or mastercard (for harvards free online cs50 course full task here). The code works for amex and mastercard but everytime i test with a 16 digit visa card it prints
VISA
INVALID
When it should only print
VISA
Here is the section of code I presume the problem to be but I can’t see why it’s not working correctly.
while (999999999999999 < card && card < 10000000000000000) // while card is 16 digits
{
card = card / 100000000000000;
if (39 < card && card < 50) // first digit is 4
{
printf("VISAn");
}
else
{
if (50 < card && card < 56) // first 2 digits are 51,52,53,54or55
{
printf("MASTERCARDn");
}
else
{
printf ("INVALIDn");
}
}
}
Any help would be greatly appreciated.
I’ve tried coding individual functions for every major step in my main code so let me know if there is not enough information to go off.
Shaz Miller is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
9