### Why isn't my algorithm working?

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. Can anyone see what's 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;
}

Putting it in code tags to help readabilty....

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384`` `````` 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; } ``````
Topic archived. No new replies allowed.