hi i have two question? if i wanted to read in file also, how do i do that?
#include <conio.h>
#include <iostream>
#include <cstring>
#include <ctype.h>
#include <string>
using namespace std;
using std::string;
enum CardType{
MasterCard,
BankCard,
Visa,
AmericanExpress,
Discover,
DinersClub,
JCB
};
bool Validate(CardType cardType, string cardNumber){
int number[16], len=0, srln = cardNumber.length();
for(int i=0; i<srln; ++i){
if(isdigit(cardNumber[i])){
if(len == 16) return false;
number[len++] = cardNumber[i];
}
}
switch(cardType){
case MasterCard:
if(len!=16) return false;
if(number[0] != 5 || number[1] == 0 || number[1] > 5)
return false;
break;
case BankCard:
if(len != 16)
return false;
if(number[0] != 5 || number[1] != 6 || number[2] > 1)
return false;
break;
case Visa:
if(len != 16 && len != 13)
return false;
if(number[0] != 4)
return false;
break;
case AmericanExpress:
if(len != 15)
return false;
if(number[0] != 3 || (number[1] != 4 && number[1] != 7))
return false;
break;
case Discover:
if(len != 16)
return false;
if(number[0] != 6 || number[1] != 0 || number[2] != 1 || number[3] != 1)
return false;
break;
case DinersClub:
if(len != 14)
return false;
if(number[0] != 3 || (number[1] != 0 && number[1] != 6 && number[1] != 8)
|| number[1] == 0 && number[2] > 5)
return false;
break;
case JCB:
if(len != 16 && len != 15)
return false;
if(number[0] != 3 || number[1] != 5)
return false;
break;
default:
cout<<"\n No such type of card exists .... !";
}
//Using the Luhn's Algorithm
int sum = 0;
for(int i=len-1; i>=0; ++i){
if(i % 2 == len % 2){
int n = number[i] * 2;
sum += (n / 10) + (n % 10);
}
else
sum += number[i];
}
return (sum % 10 == 0);
}
void valid(){
cout << "The credit card number is VALID !";
}
void invalid(){
cout << "The credit card number is INVALID !";
}
int main(){
system("cls");
string ccn, ct;
cout << "Enter the Card Type :";
cin >> ct;
cout << "Enter the Credit card Number :";
cin >> ccn;
CardType card = Visa;
bool val = Validate(card, ccn);
if (val == true) valid();
else invalid();
system("pause");
return 0;
}