Monthly Archives: March 2014

Credit Card Project

For Computer Science 220 at the University of Mary Washington, I created a project in C++ that validates credit card numbers based off of three mechanisms. The first mechanism to validate the credit card number is checking the length of the number. This process took in the name of the card and compared the length of the number to the correct length for that card. If the credit card did not pass this stage it did not continue on to the final two mechanisms. It instead would print to the screen “invalid – length”. The next mechanism to validate the credit card number is checking the prefix of the number. This process took in the name of the card and compared the first, according to Visa, or first and second, according to American Express and MasterCard, numbers to the correct prefixes for that card. If the credit card did not pass this stage it did not continue on to the final mechanism. It instead would print to the screen “invalid – prefix”. The final mechanism to validate the credit card number is Luhn’s algorithm. For Luhn’s algorithm the number needs to be reversed, every other number starting with the second number gets doubled, each individual digit is added together, and if the integer that results is a multiple of ten then the number is valid. If the credit card did not pass this stage the card number was not valid. It would print to the screen “invalid – luhns”. If the credit card number passed all three of the mechanisms it was a valid card and “valid” would be printed to the screen.

 

This project was definitely the hardest project I have worked on for multiple reasons. First, this project took me about two-three weeks to complete while working on different pieces every day. I usually spend about two-three days on a project and am done. I am glad this program took a longer time to work through and complete because it is more like real, job-related projects that I will eventually face in my career. Second, this program relied heavily on functions which I haven’t used in about two years. The re-introduction to functions wasn’t too bad however, applying what I was taught in a language that I have never used was rather difficult. Third, many times I would try something and it wouldn’t work correctly. For example, in Luhn’s algorithm I had to write code to reverse a string of numbers. I had designed a loop to reverse the string manually that seemed like it should have worked. However, each time I ran the program the string would reverse correctly but not be usable in the next steps of Luhn’s algorithm. When I took my source code to my professor she thought it would work too but instead of helping me try to fix it just googled a solution that worked completely fine. I never even thought about googling my problems because I typically like to fix them myself but I learned that sometimes I just need to google something because the solution to it is beyond my knowledge, temporarily.

 

While completing this project I took it one piece at a time. The first element I worked on was completing the part in main that controlled input and output. Once I finished that element I moved onto writing the function that checked the length of the card number. I added the call to this function into main so I could test it. Once I finished that element I moved onto writing the function that checked the prefix of the card number. I added the call to this function into main so I could test it. Once I finished that element I moved onto writing the function that checked for Luhn’s algorithm. This function gave me the most problems but once I finished it the project moved a lot quicker. I added the call to this function into main so I could test it. Once I finished that element I worked on fixing main so it would stop applying checks once one of the checks found the card to be invalid. The only thing left after that was making main read input from a file so I wouldn’t have to type in the card names and numbers repetitively. I then verified that the program was running correctly by testing the output with an input file I made to ensure the program returned the correct output that I was expecting.

The code for this project is available at: Project 2 E-portfolio Code