To write a program which helps validate a possible credit card number you will need to know two things. Some basic programming skills and the Luhn algorithm.
According to online sources the Luhn algorithm or modulus 10 was created by IBM scientist Hans Peter Luhn. The end result of his discovery, patented in 1960, is one that adds a level of integrity to a number which we can use for error reduction. A number known to be generated with this algorithm can be tested for errors after transmitting or recording using this simple method. These types of algorithms act a bit like hidden signatures. An alteration to a number generated with this algorithm is very very likely to change the signature and this change indicates the error. That’s basically it. All credit card numbers are defined with this algorithm built in.
Here it is in action using possible card number 4388576018410707 as an example:
If the result from step 3 is divisible by 10 then your number is considered valid!
The following is a simple program which performs this check.
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 |
//Author : Daniel Touma (100147753) //Version : 0.1 //Name : Credit Card Number Validator //Description : Checks for a numbers adherence to the Luhn algorithm #include <iostream> #include <string> using namespace std; int main() { string cardNumber; //this will carry the number given by the user. int sum(0), sumOfDoubleEven(0), sumOfOdd(0), evenDigit(0), oddDigit(0); cout << "Welcome to the Credit Card Number Validator." << endl; cout << "This program checks for a numbers adherence to the Luhn algorithm" << endl; //Main loop do { cout << endl << "Please provide a number to validate or type 'n' to quit "; cin >> cardNumber; //gets cardNumber from user if (cardNumber == "n") break; //exits loop on user request //this for loop repeats once for each digit of the cardNumber. //digitPosition decrements by one on each pass from last position to first position for (int digitPosition=cardNumber.length(); digitPosition>0; digitPosition--) { if (digitPosition%2 == 0) { //executes the following code if digitPosition is even oddDigit=(int)cardNumber[digitPosition-1]-'0'; sumOfOdd=sumOfOdd+oddDigit; } else { //executes the following code if digitPosition is odd. evenDigit=((int)cardNumber[digitPosition-1]-'0')*2; evenDigit=evenDigit%10+evenDigit/10; sumOfDoubleEven=sumOfDoubleEven + evenDigit; } } sum=sumOfOdd+sumOfDoubleEven; //sums the result cout << endl <<"The number your provided is "; if (sum%10==0) //executes if sum is divisible by 10 cout << "valid" << endl; else //executes if sum is not divisible by 10 cout << "invalid" << endl; }while (cardNumber != "n"); return 0; } |
I have not restricted the size of the number which allows the user to enter a number of almost any size. As you can see the entire program is written inside the main loop, without the use of functions or classes. If you are curious you can see this program rebuilt with functions in an article demonstrating c++ modularity.
Leave a Reply