Why isn't my algorithm code working?

  • Thread starter SMA_01
  • Start date
  • #1
218
0
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;
}
 

Answers and Replies

  • #2
33,719
5,417
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.



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.
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.
Code:
bool isValid(string creditCardNumber)
{
   int totalSum;
   int evenSum;
   int oddSum;


   doubleEvenSum(creditCardNumber); // Why is this here? 

   sumOddDigits(creditCardNumber); // Ditto. Both functions are supposed to return a value.
                                                // Calling them like this causes the return value to be discarded.

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

   totalSum = evenSum + oddSum;

   modTen(totalSum); // You should not call this function like this, because it doesn't do anything useful.

   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;
}
In doubleEvenSum, you do this:
Code:
countLength = creditCardNumber.length()-2;
In sumOddDigits, you do this:
Code:
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.
 
  • #3
218
0
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).
 
  • #4
33,719
5,417
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:
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.
 
  • #5
218
0
Got it working, thank you!
 

Related Threads on Why isn't my algorithm code working?

  • Last Post
Replies
10
Views
838
Replies
4
Views
2K
Replies
1
Views
1K
Replies
9
Views
1K
Replies
14
Views
1K
  • Last Post
Replies
6
Views
13K
  • Last Post
Replies
2
Views
4K
  • Last Post
Replies
9
Views
783
Top