# Problem with credit.c concerning if loops with multiple initial conditions

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");
}
}
``````

Are you sure `first_digits` and `first_two_digits` are giving you what you want?

`digits` is the number of digits entered into cc_number. So if user enters 15 (or 16 or 13) digits, isn't `first_digit` always = 1?

Example: User enters `378282246310005` which is 15 digits, so `digits = 15` and `first_digit = digits / 10` equals......

You need an equation that uses `digits` to determine which power of 10 to use to determine the first (or first two) digit of `cc_number`

• The user doesn't enter 13, 15, or 16 followed up by numbers. Instead, the user types a number that can start with any two digits not just the ones referenced above.
– user22842
Commented Dec 9, 2018 at 18:34
• Sorry if I wasn't clear. Clarification added to answer. Commented Dec 9, 2018 at 18:47
• Okay I changed the 'int first_digit = digits / 10;' to 'int first_digit = m / pow(10, (digits - 1));' and the 'int first_two_digits = digits / 100;' to 'int first_two_digits = m / pow(10, (digits - 2));' and it worked. m is equal to cc_number a. Thanks for your help :)
– user22842
Commented Dec 9, 2018 at 19:07

Go through the notes and Walkthroughs. Please be careful with the number of digits.I think that's where you are going wrong.