0

so I have a problem with the Credit exercise from pset1. I updated my code today after realizing that I didn't split up the digits of the product of the current digit in the card number and 2 and adding them to the checksum individually. Now, my checksum is absolutely going haywire and I don't know what the problem is/how to correct the problem?

The code I used:

//checks credit card numbers for their validity
//and for the "brand" of the card, for example Visa,etc via Luhn's algorithm.

#include <stdio.h>
#include <cs50.h>

int main(void)
{

    //gets and prints card number
    long card_num = get_long("Number: ");


    //variable for number's length
    int num_length = 0;

    //duplicate of card num variable for checksum and length calculation purposes
    long card_num_dup = card_num;


    //while loop to determine the length of the number
    while (true)
    {
        card_num_dup /= 10;
        num_length++;
        if (card_num_dup < 1)
        {
            break;
        }
    }
    //end of while

    //reinitialize duplicate variable
    card_num_dup = card_num;

    //checking the card's type/validity

    if (num_length > 16 || num_length < 13)
    {
        //checks for invalid lengths
        printf("INVALID\n");
    }
    else
    {

        //variable for the checksum
        int checksum = 0;

        //loop counter
        int j = 0;

        //variable for the current digit in the number
        int current_digit = 0;

        //for loop for looping through the number to calculate checksum
        for (int i = 0; i < num_length; i++)
        {

            //get current digit and remove it from card num
            current_digit = card_num % 10;
            card_num /= 10;

            //if to control the digits' processing via Luhn's algorithm
            if (j % 2 != 0)
            {
                int multiply = current_digit * 2;
                int multiply_length = 0;

                //while loop to determine length of multiply variable
                while (true)
                {
                    multiply /= 10;
                    multiply_length++;
                    if (multiply < 1)
                    {
                        break;
                    }
                }
                //end of while loop

                //for loop for looping through the multiply variable
                for (int k = 0; k < multiply_length; k++)
                {
                    int digit = multiply % 10;
                    multiply /= 10;
                    checksum += digit;
                }
                //end of for loop
            }
            else
            {
                checksum += current_digit;
            }
            //end of if

            j++;
        }
        //end of for loop

        printf("%i",checksum);

Thanks in advance for the help!

Kind Regards
PrimeBeat

1

Once the program has determined if the sum should increase by current_digit or something else, the something else takes a complicated approach. Instead of nesting another loop to work out multi-digit numbers, realize that 'multiply' can only have values in the range of 0 to 18. Consider this simpler code:

if (j % 2 != 0)
{
    int multiply = current_digit * 2;
    if (multiply < 10)
    {
        checksum += multiply;
    }
    else
    {
        // TODO: checksum += <something>
    }
}

If you prefer to keep the more complicated implementation, consider that multiply has been divided by 10 until it's 0. Recalculating

multiply = current_digit * 2;

before the for loop would be necessary. Or make a copy before dividing it down.

You must log in to answer this question.

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