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....

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84

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.