I have signed up for this year's CS50x, and I am stuck on the problem where we are meant to validate a credit card. The problem requires that we check the amount of digits, the first digits and use Luhn's Algorithm. We then decide which company it comes the card comes from and weather it is valid or not.
Below is my code. If the number is invalid the code detects it and says that it is invalid, however if the number is a legit number nothing is printed out. I am not sure what is wrong. I'm very very new to C, I've only had some prior experience with Java and HTTPS/CSS. Any help will be appreciated. Thank you!
I understand that this course is very old, and I know that these questions have been asked before. However I wanted to use my own method and things I found around the internet to solve the problem. I feel like on the surface this should work, but I really have no clue what is wrong. Thank you for any time spent on this!
#include <stdio.h>
#include <cs50.h>
int main(void)
{
//asks for the credit card number
long number = get_long("Enter Card Number: ");
int count;
long temp3 = number;
//counts the number of digits in the card
for (count = 0; temp3 != 0; temp3/=10)
{
count++;
}
long tempnum = number;
int checksum = 0;
//this checks if the digits match the count and performs luhers algorythm
if((count == 13) || (count == 15) || (count == 16))
{
for(int i = 1; i >= count; i++ )
{
int digit = tempnum % 10;
if( i % 2 == 0 )
{
if(digit * 2 > 9)
{
checksum += (digit * 2) - 9;
}
else
{
checksum += digit * 2;
}
}
else
{
checksum += digit;
}
tempnum = tempnum/10;
}
}
else
{
printf("INVALID\n");
}
//this while loop is used to reduce a number to the first digits from the left to use to id the company
long tempnum2 = number;
while (tempnum2 >= 100)
{
tempnum2 = tempnum2 / 10;
return tempnum2;
}
long firstdigit = tempnum2;
//final if loop that states what company card this is from
if ((firstdigit == 34 || firstdigit == 37) && (count == 15) && (checksum % 10 == 0))
{
printf("AMEX\n");
}
else if ((firstdigit > 50 && firstdigit < 56) && (count == 16) && (checksum % 10 == 0))
{
printf("MASTERCARD\n");
}
else if ((firstdigit == 4) && (count == 13 || count == 16) && (checksum % 10 == 0))
{
printf("VISA\n");
}
}