Subscribe to Bodom's Universe        RSS Feed
-----

Credit Card Verifier

Icon 2 Comments
So i was lookin through Martyr's list of program ideas, when i ran across a credit card verification algorithm.

I was quite intriqued... quite indeed...
So, I wrote a program that will verify 3 different credit cards, Mastercard, visa, and AMEX.

I posted the algorithm for the Mod10 part of it under snippets... I hopes it gets accepted *crosses fingers in anxiety*

Anyway, Here it is.

#include<iostream>
#include<string>
using namespace std;

int card(void);
bool validate(int cType);
bool mod10(int* number, int len);

int main(void)
{
	if(validate(card()))
		cout << "Card is valid";
	else cout << "Card is invalid";
	return 0;
}

int card(void)
{
	int choice;
	
	cout << "Choose credit card type:" << endl;
	cout << "1. Visa" << endl;
	cout << "2. Mastercard" << endl;
	cout << "3. AMEX" << endl;
	cin >> choice;
	
	return choice;
}

bool validate(int cType)
{
	string num;
	int* number;
	int len;
	cout << "Enter credit card number: ";
	cin >> num;
	len = num.size();
	number = new int[len];
	for(int i = 0; i < len; i++)
		number[i] = static_cast<int>(num[i]) - 48;

	switch(cType){
		case 1:
		if (len != 16 && len != 13)
			return false;
		if (number[0] != 4)
			return false;
		if (!mod10(number, len))
			return false;
		break;
		case 2:
		if (len != 16)
			return false;
		if (number[0] != 5)
			return false;
		if (number[1] > 5 || number[1] < 1)
			return false;
		if (!mod10(number, len))
			return false;
		break;
		case 3:
		if (len != 15)
			return false;
		if (number[0] != 3)
			return false;
		if (number[1] != 5 && number[1] != 7)
			return false;
		if (!mod10(number, len))
			return false;
		break;
	}
		
	return true;
}

bool mod10(int* number, int len)
{
	for(int i = 1; i < len; i += 2)
		*(number + i)*= 2;
	
	int sum = 0;
	for(int j = 0; j < len; j++)
	{
		if(j == 1 || j%2 == 1)
			sum += *(number + j)/10 + *(number + j)%10;
		else
			sum += *(number + j);
	}
	
	if(sum%10 == 0)
		return true;
	else return false;
}



Basically what i have done is set up 3 functions, excluding main, to handle user input and then verification. Main is actually quite small, which I find immensly enjoyable.

The user is first asked to enter which kind of card he/she will be entering, and then the credit card number (notice how there is nothing stored to a file or anything... i'm not out to teach you how to rip someone off... but if you do, this would be the place to do so :P)

so........
now that we have that, we check the number of digits, the leading digit or digits, and then we go into the beautifully simple algorithm.
see if you can figure it out for yourself... an explanation will be given once the code snippet is accepted, if it is.

Cheers
~Bodom

Questions or comments, please feel free to comment.

2 Comments On This Entry

Page 1 of 1

Martyr2 

03 January 2009 - 05:33 PM
Super cool and good job. Glad my ideas post could be of some inspiration. I was hoping that it would spur some creative solution like this. :^:
0

bodom658 

03 January 2009 - 06:30 PM
lol thanks for the inspiration and thanks for reading! I'll probably grab another off ur list...
0
Page 1 of 1

January 2022

S M T W T F S
      1
2345678
9101112131415
161718192021 22
23242526272829
3031     

Recent Comments

Search My Blog

13 user(s) viewing

13 Guests
0 member(s)
0 anonymous member(s)