1.11M Members

credit card numbber

 
0
 

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;
}
 
0
 

Use the iostream library to read in data from the file into a card type and card number. How best to do that will depend on how the information is arranged in the file. A common way to set up a file would be to have two fields per line, separated by spaces or a tab. See example fil contents below.

MasterCard 2222222222222222
Visa 4444444444444444
Discover 3333333333333333

In such a case, you could replace lines 109 to 112 with something like the following...

ins >> ct >> cn;

where ins is an open ifstream object set up to read from an in input file earlier in the program.

ifstream ins;
ins.open("creditcards.txt");

The ins >> ct >> cn; line will read ONE record from the file. You might want to set up a loop where you read continually until there are no more records in the file.

As an aside, you do not use the ct variable anywhere. You read in the credit card type from the user, but then you never do anything with what you read in (the ct variable), but instead set up a card variable and assume the card is a Visa.

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article