C++ Credit Card Validator
Continuing my (almost) daily C++ practice, I am now making a program to check if a credit number is valid or not.
The first step was to use the Luhn algorithm, researching this, I learned it was a simple and effective fraud protection algorithm.
I broke in down into a few simple steps using this formula and got some test credit card numbers online and attempted the math. It took a little while to get used to, but I got used to it quickly enough.
Naturally doing the math is tedious, so I took to Visual Studio 2022 and booted up a C++ file. At first, I created some functions to use later. These were for getting the digits and the sum of the odd and even numbers.
Then I created some declarations for each of the functions.
I then added some functionality to the main code, mainly prompting the user to input their number and allowing them to do so. Then as a result, adding the sum of the odd and even digits together.
For the sum of the even digits, I created a for loop and as I was dealing with a card number, using the Luhn algorithm, I had to start from the reverse. I got the length of the card number using .size. Given arrays start at 0, I had to subtract 2 to get the second to last digit.
I then ensured the loop continues if i is less than or equal to zero. Then is decremented by minus 2 because I needed the even digits. I took the sum of plus or equal the GetDigit function. The argument was cardNumber at [i] multiplied by 2. I then subtracted the character of zero. Okay, this bit confused me a bit, but as I understand it, the ASCI shows a value of zero that is equivalent to 48. By subtracting by zero (maybe also 48?) I get the numbers 0-9 and multiplying by 2 because the number could be 2 digits.
This meant, I needed to alter the GetDigit declaration because the digit I was working with was indeed doubled. Since the number was 18, I needed to get 1 and 8. I returned number modulus 10. Then I added the number divided 10 % 10. This is what split the two numbers. This part did cause a lot of confusion, apologies if I'm scatterbrained as I explain it, I know what I'm trying to say!
With the functions complete it was time to finish this off. I created an if statement that if the result modulus 10 is equal to zero then it is valid, if not then it isn't!
Then I used a test credit card number to test if it was valid. Since I already used the Luhn algorithm to ensure it was, I could see if the code was working....which it was!
And there it is! Short and simple. I wanted to apply an algorithm to a piece of code and learn a couple of new operators and succeeded in that. I think I'm still not great at explaining myself, but this is only day 2 of my (almost) daily C++ challenge to myself. I'll get better!
Github Link: https://github.com/TripleHitcombo/PracticeCode/blob/main/C%2B%2B%20Credit%20Card%20Validator.sln
Github Link: https://github.com/TripleHitcombo/PracticeCode/blob/main/C%2B%2B%20Credit%20Card%20Validator.sln
Comments
Post a Comment