Project 2!

Have you ever wondered how credit cards are validated? Well for my second project in CPSC 220 we had to write a program that took  three steps validate a credit card. The three steps were length, prefix, and Luhn’s Algorithm. All credit cards must pass all three steps in order to be a valid card. […]

Have you ever wondered how credit cards are validated? Well for my second project in CPSC 220 we had to write a program that took  three steps validate a credit card. The three steps were length, prefix, and Luhn’s Algorithm. All credit cards must pass all three steps in order to be a valid card. Each card type (IE Visa, MasterCard, and AmericanExpress)  has it’s own set of “rules” for each validation step. In the length step Visas must be 13 or 16 digits, MasterCards must be 16 digits, and AmericanExpress must be 15 digits. If the card passes this validation step it moves on to the prefix step. For a card to be valid in this step Visa cards prefix is “4” , MasterCard is ” 51, 52, 53, 54, or 55″, and AmericanExpress is ” 34 or 37″. If the card passes this step then it moves on to Luhn’s Algotithm. In Luhn’s Algotihm The credit card number is reversed. Now double the digits in the even positions; some of the results will be two digits, that is okay. Now separate all the two digit numbers (IE 10 would become 1 0). Once that is done add all the digits up, if the sum is dividable by 10 then the card is valid. If at any point the card fails to verify then the program should print out where it failed and not continue any other test with that card.

On this project I have to read a file, create all three validation steps, and have it print out to the screen. Each of the validation steps were created as functions (these were a first). This program was a huge challenge for  myself. I learned lots of stuff. First was how to open and read in an unknown length file with a “while” loop. Then was how to write the functions and implement them. The hardest function to write was the Luhn’s Algotihm. However, I did make a mistake on the prefix step but that has been corrected since then.

Here is a video of the program running.

CreditCard Validation

Here is what the end product looks like! Here is my code for this project.

/**
* ccvalation.cpp
* <p>
*
*  The purpose of this program is to validate a credit card from a file
*  and use three methods to see if the card is valid.
*
* author: James Moore
* email: jmoore7@mail.umw.edu
* language: C++
* pledge: James Moore
*
*/

#include <fstream>
#include <iostream>
#include <cmath>
#include <iomanip>
#include <string>
using namespace std;

int main ( )
{
//prototypes
bool cardLength(string, string);         // card lenght function
bool cardPrefix(string, string);         // Card number satisfies the prefix
bool algorithmLuhn(string, string);      // Luhn's algorithm

string filename;
cout << "Please enter the filename:"<< endl;
cin >> filename; // enter the file name

// open file
ifstream fcin; // delcares file stream
fcin.open(filename.c_str());

int count;
string cardType;    // credit card type
string cardNumber;  // credit card number
count = 0;          // intializes the while loop
fcin >> cardType >>  cardNumber;  // cin the credit card type and number
while(fcin){
cout.setf(ios::left, ios::adjustfield); // this is for the print out
cout << setw(20) << cardType;           // this is for the print out
cout << setw(20) << cardNumber;         // this is for the print out

int result = cardLength(cardType, cardNumber);  // function call
if (result == 1){                            // if the fuction returns true continue
int result = cardPrefix(cardType,cardNumber); // function call
if (result == 1) {                      // if the fuction returns true continue
int result = algorithmLuhn(cardType, cardNumber); // function call
if (result == 1) {                // if the fuction returns true continue
cout.setf(ios::left, ios::adjustfield);   // this is for the print out
cout << setw(20) <<" Valid"<< endl;       // this is for the print out
}
else {                                // if the function is not true print this
cout << setw(20) << " Invalid - Luhn's"<< endl;
}
}
else {                                     // if the fuction returns true continue
cout  << setw(20) << " Invalid - Prefix"<< endl;
}
}
else {                                          // if the fuction returns true continue
cout << setw(20) << " Invalid - Length" << endl;
}

count ++;         // add 1 to count
fcin >> cardType;    // pull in a new credit card type
fcin >> cardNumber;  // pull in a new credit card number
}// end while loop

//close file
fcin.close();

return 0;

} // end main

// this test the cards prefix

bool cardPrefix (string Type, string Number){ // function

if ((Number == '4') && (Type == "Visa")) {  // test the Visa Card prefix
return true;
}

else if ((Number == '3') && ((Number == '4') || (Number == '7')) // test the AmericanExpress Card prefix
&& (Type == "AmericanExpress")) {
return true;
}
else if ((Number == '5') && ((Number == '1') || (Number == '2')
|| (Number == '3') || (Number == '4') || (Number == '5')) // test the MasterCard Card prefix
&& (Type == "MasterCard")) {
return true;
}
else {
return false;
}
}

// this tests the cards length
bool cardLength (string Type, string Number){  // function
int length;
length= Number.length();     // find what the length of the srting is

if ((length != 13 && length !=16) && Type == "Visa"){    // test the Visa Card Length
return false;
}
else if ((length != 15) && Type == "AmericanExpress"){   // test the AmericanExpress Card Length
return false;
}
else if ((length != 16) && Type == "MasterCard"){        // test the MasterCard Card Length
return false;
}
else {
return true;
}

}

// this is the test for Luhn's
bool algorithmLuhn(string Type, string Number){ // function
int sum=0; // stores the total
int len = Number.length();// card length
int result;
int even =0; // stores the total of the "even" numbers
string rNumber; // reversed cardNumber

rNumber = string(Number.rbegin(), Number.rend()); // reverse code
for (int a = 0; a < len ; a ++){
if (a % 2 != 0 ){
result = (rNumber.at(a) - '0') *2;  // this takes all the "even" numbers and multiples them by 2
if ( result > 9){                   // if the value is larger than 10 then subtract it by 10 then add 1
even = (result - 10) + 1;
sum = sum + even;                   // this adds all the values together
}// end if
if (result < 9){                    // if the "even" number is less than 9 just add it with the number before
sum = sum + result;
}// end if
}// end if
else {  sum = (rNumber.at(a) - '0') + sum;  // this takes all the "odd" numbers and add them together
} // end else
}// end for loop

if (sum % 10 != 0){                            // this test to see if it is divisable by 10 if it has a remainder then it is false if not it is true
return false;
}
else {
return true;
}

}

If you have any questions please feel free to contact me at jmoore7@mail.umw.edu

James

Posted on Categories DGST 201