0

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

You shouldn't have return tempnum2; in this while loop:

while (tempnum2 >= 100)
{
    tempnum2 = tempnum2 / 10;
    return tempnum2;
}

The variable tempnum2 will keep on updating just through assignment.

Once this is removed, some legit numbers start getting printed. So you are getting there! However, they aren't printed in every instance. To identify the remaining bug, have a look at which company's numbers aren't getting printed, what is distinctive about how its numbers begin, and the condition under which the same while loop mentioned above finishes dividing by 10 ;-)

2
  • Thank you for your help! I re-wrote the while code again, and now everything is sorted out.
    – Vedant
    Jul 15 at 11:56
  • Excellent, well done. If my answer was helpful for solving your problem, could you accept it by clicking the tick? This isn't just for my benefit - it flags up to future users of this site that they'll find a potential solution to their own problem if they look at your question. Jul 16 at 12:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .