I am working on the credit.c code. I am trying to make sure that the number of digits and the first digits of each credit card number value that is entered correspond with the brand that credit card is from. I am trying to use if and else if loops to determine this. I have still not gotten to the part of incorporating Luhn's algorithm into my code. My main problem is that whenever I put in a valid American Express, MasterCard, or Visa credit card number, the result is "INVALID\n". In my code below, I have put comments where my problem occurs. Thanks for the help!
#include <stdio.h>
#include <cs50.h>
int main(void)
{
long long cc_number;
int digits = 0;
do
{
cc_number = get_long_long ("Credit Card Number: "); //Asks user for credit card number
}
while (cc_number <= 0);
long long n = cc_number;
while(n > 1) //This loop counts the number of digits that were inputed into the original string.
{
n /= 10;
digits++;
}
int first_digit = digits / 10; //Creates a variable that determines the first digit of the credit card number. Example: 375098517920949 whould have a fist_digit value of 3
int first_two_digits = digits / 100; //Creates a variable that determines the first two digits of the credit card number. Example: 375098517920949 would have a fist_two_digits value of 37
if ((digits != 15) && (digits != 16) && (digits != 13)) //Ensures that only cards with 13, 15, and 16 digits are validated.
{
printf("INVALID\n");
}
//Main Problem starts with the following if loop:
if (digits == 15 && (first_two_digits == 34 || first_two_digits == 37)) //If there are 15 digits in cc_number and the first two digits are 34 or 35, print "AMEX." From here down, the code is not working
{
printf("AMEX\n");
}
else if (digits == 13 && (first_digit == 4)) //If there are 13 digits in cc_number and the first digit is 4, print "VISA." Problem continues here
{
printf("VISA\n");
}
else if (digits == 16) //If there are 16 digits determine whether "MASTERCARD" or "VISA" is printed. Problem continues here
{
if (first_two_digits == 51 || first_two_digits == 52 || first_two_digits == 53 || first_two_digits == 54 || first_two_digits == 55) //If there the first two digits are 51,52,52,54, or 55, print "MASTERCARD"
{
printf("MASTERCARD\n");
}
else if (first_digit == 4) //If there the first digit is 4, print "MASTERCARD"
{
printf("VISA\n");
}
else
{
printf("INVALID\n")
}
}
else
{
printf("INVALID\n");
}
}