1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Why isn't my algorithm code working?

  1. Oct 20, 2012 #1
    I have to write a program validating credit card numbers using Luhn's algorithm. I am having trouble getting my last function to work. For a credit card number i'm supposed to double every second digit from right to left, and sum them (note double digits, like 10=1+0), and then add the rest of the digits together, and total both sums. After that, I make sure it's divisible by 10. Is there anything wrong with my code? Thank you.


    bool isValid(string creditCardNumber)
    {
    int totalSum;
    int evenSum;
    int oddSum;


    doubleEvenSum(creditCardNumber);

    sumOddDigits(creditCardNumber);

    evenSum = doubleEvenSum(creditCardNumber);
    oddSum = sumOddDigits(creditCardNumber);

    totalSum = evenSum + oddSum;

    modTen(totalSum);

    if (modTen(totalSum))
    return true;
    else return false;
    }


    int doubleEvenSum(string creditCardNumber)
    {
    int evenSum;
    int countLength;
    char charVal;
    int intVal;
    int doubleIntVal;


    countLength = creditCardNumber.length()-2;
    evenSum = 0;
    while(countLength>=0)
    {
    charVal = creditCardNumber.at(countLength);

    intVal = charVal - 0;

    doubleIntVal = 2 * intVal;

    if (doubleIntVal > 9)
    {
    doubleIntVal = doubleIntVal - 9;
    }

    evenSum = evenSum + doubleIntVal;
    countLength = countLength - 2;
    }
    return evenSum;
    }


    int sumOddDigits(string creditCardNumber)
    {
    int oddSum;
    int countLength;
    char charVal;
    int intVal;


    countLength = creditCardNumber.length()-1;
    oddSum = 0;
    while (countLength>=0)
    {
    charVal = creditCardNumber.at(countLength);
    intVal = charVal - 0;
    oddSum = oddSum + intVal;
    countLength = countLength - 2;
    }
    return oddSum;
    }


    bool modTen(int totalSum)
    {
    if (totalSum % 10 == 0)
    return true;
    else return false;
    }
     
  2. jcsd
  3. Oct 20, 2012 #2

    Mark44

    Staff: Mentor

    First off, when you post code here, put a [ code ] tag at the top and a [ /code ] tag at the bottom (both without extra spaces that I showed). This preserves any indentation you have used, which makes your code easier to read.
    Second, USE INDENTATION!!! The compiler doesn't care if you use indentation, but if you use indentation appropriately, it's easier for human readers to follow your code.



    Did you try compiling your code? If there are syntax errors in your code, the compiler will give a list of errors. The compiler won't tell you about errors in your logic, though.

    Look for my comments throughout your code.
    In doubleEvenSum, you do this:
    Code (Text):
    countLength = creditCardNumber.length()-2;
     
    In sumOddDigits, you do this:
    Code (Text):
    countLength = creditCardNumber.length()-1;
     
    Why are they different? Are these two functions supposed to do the same things except that one works on the digits in the even places, and the other works on the digits in the odd places? If so, why did you subtract 2 in one function and subtract 1 in the other? A comment would have helped explain your logic. Also, if both functions are intended to do similar things, you should give them names that are more similar.
     
  4. Oct 20, 2012 #3
    Sorry, I did use indentation, I guess I should have fixed it beforehand.

    You are right about calling those functions, I fixed that thanks.

    In doubleEvenSum, you doulble every second digits from right to left. and then add them. For example, for a number of length 16, the first position you would begin is 14:

    4 1 3 0 1 3 2 5 6 4 7 9 8 5 3 2
    0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

    And for odd it you would start at first place to the right, so at position length-1.

    My problem, is that even if I enter a valid number, the calculations deem it invalid, so I am not sure what is off with my code (I tried it again after I fixed the part you mentioned).
     
  5. Oct 20, 2012 #4

    Mark44

    Staff: Mentor

    You haven't said what language you're using - I suspect it's Java.

    In doubleEvenSum and in sumOddDigits, you have some code that I think is incorrect.
    Code (Text):
    intVal = charVal - 0;
     
    If you want to convert a digit character to a number, subtract the character '0', not the number 0.

    If that doesn't fix things, please give me an example of what doubleEvenSum should do to a number. Your explanation wasn't enough to give me an idea of the algorithm you're trying to implement.

    Also, if there is a debugger available, it's a good investment of your time to learn how to use it. Otherwise, you can fall back to the way things were done in the old days - adding temporary print statements through your code to print the values of variables. When your code is running as expected, you can remove the print statements that aren't needed any more.
     
  6. Oct 21, 2012 #5
    Got it working, thank you!
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook