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.
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
)
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.
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
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.
Page 1 of 1
← January 2022 →
| S | M | T | W | T | F | S |
|---|---|---|---|---|---|---|
| 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 |
Tags
- Adminsitration
- Apple
- assembly
- atmel
- AVR
- avr-libc
- Bada
- beginner
- Blackberry
- boot
- C
- C#, .NET, and XNA
- C++
- ccleaner
- code
- defragmentation
- Desktop
- dynamic
- embedded
- FooBada
- foobar
- foobar2000
- Gnome
- high
- interrupts
- Java
- level
- Linux
- Math
- Matricies
- Matrix
- microcontroller
- nasm
- New
- Open Source
- Operating System
- Oracle
- oscilloscope
- Programming
- Python
- Random Computer Stuff
- Random Rants
- Repair
- Samsung
- sector
- serial
- speed
- Square
- static
- timer
- UART
- Ubuntu
- unix
- USART
- virtualbox
- Visual
- VOIP
- Windows
- Windows 7
- x86
My Blog Links
Recent Entries
-
Playing in Pythonon Apr 24 2011 10:06 PM
-
-
Using Virtualbox as a bootloader testing environmenton Nov 13 2010 11:00 PM
-
AVR Oscilloscope, Part 1on Sep 13 2010 07:21 PM
-
My thoughts on Bada as an Operating System and as a Development Platformon Sep 05 2010 10:43 AM
Recent Comments
Search My Blog
13 user(s) viewing
13 Guests
0 member(s)
0 anonymous member(s)
0 member(s)
0 anonymous member(s)



2 Comments









|