I started edX CS50 and I'm working on a program that checks if a credit card number is valid by checking that it has the right amount of digits, the starting digits are correct, and then using luhns algorithm and returns either the card name if it is valid or invalid. It all works for the most part however, the valid visa cards i've entered return as invalid and some american express numbers don't give a read back. i'm assuming the problem is in the if statements but ill provide a a copy of the file. If anyone could help it would be much appreciated.
C:
//starts checks to see if the card is valid
//validates if it is american express card
if (count == 15) //total digits
{
if (num == 34 || num == 37) //starting numbers of cards
{
if (sumt % 10 == 0) //luhns algorithm
{
printf("AMEX\n");
}
}
else
{
printf("INVALID\n");
}
}
//validates if it is mastercard
else if (count == 16) //total digits
{
if (num == 51 || num == 52) //starting digits
{
if (sumt % 10 == 0) //luhns algorithm
{
printf("MASTERCARD\n");
}
}
else if (num == 53 || num == 54) //starting digits
{
if (sumt % 10 == 0) //luhns algorithm
{
printf("MASTERCARD\n");
}
}
else if (num == 55) //starting digits
{
if (sumt % 10 == 0) //luhns algorithm
{
printf("MASTERCARD\n");
}
}
else
{
printf("INVALID\n");
}
}
//validates if its visa
else if (count == 13 || count == 16) //total digits
{
if (vnum == 4) //first digit
{
if (sumt % 10 == 0) //luhns algorithm
{
printf("VISA\n");
}
}
}
else
{
printf("INVALID\n");
}
Attachments
Last edited by a moderator: