Question
I need help debugging my program. It is a program using Luhn's algorithm to validate a 16 digit credit card. The program assumes the card
I need help debugging my program. It is a program using Luhn's algorithm to validate a 16 digit credit card. The program assumes the card number is 16 digits long. The output is supposed to either state invalid or valid.However, when I put it in a valid credit card number, it still outputs it as invalid.
For Reference:
To validate a credit card number, you perform the following steps:
Loop through the number one digit at a time.
The digits in the odd-numbered digits are accumulated.
The digits in the even-numbered digits are split into tens and one, these are added together, and then all of them are accumulated.
When done looping, add the accumulated odd and even accumulations together. If the result is evenly divisible by ten, the number is valid.
Here is my code:
#include
#include
#include
int stripper(long long longNumber, int digitNumber) {
return ((longNumber - ((long long)(longNumber / pow(10, digitNumber))*pow(10, digitNumber))) / pow(10, digitNumber - 1));
}
int isOdd(long int digit)
{
if (digit % 2 == 0)
return 0;
else
return 1;
}
int splitTens(int num) {
return ((num / 10) % 10);
}
int splitOnes(int num) {
return (num % 10);
}
int main() {
long long testNumber, sum = 0;
int i, currDig;
printf("Please enter credit card number ");
scanf_s("%lld", &testNumber);
for (i = 1; i
if (isOdd(i) == 0) {
// even location digit
sum += stripper(testNumber, i);
}
else {
// odd location digit, double the number and add digits of of the number after doubling
currDig = stripper(testNumber, i);
currDig *= 2;
sum += splitOnes(currDig) + splitTens(currDig);
}
}
if (sum % 10 == 0)
printf("Valid Credit Card Number");
else
printf("Invalid Credit Card Number");
getchar();
getchar();
return 0;
}
Here is what I am getting entering a valid credit card number
CAUsersewe Docurrments\CreditCard1DebugCreditCard1.exe Please enter credit card number 5126415296389632 Invalid Credit Card Numbe Menut O Type here to search 12:02 PM R3/21/2018Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started