我不确定为什么变量的代码已知。它总是翻2倍。代码应该显示Times处理过的有效卡、无效卡和未知卡。已知的卡是美国运通卡、discover卡、visa卡和master卡。我试图得到它们的计数,但似乎它们总是因为某种原因翻倍
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
void validateCC(string);
string checkCC(string, int, bool&);
bool validateCCNum(string);
string setCCType(string);
int proc = 0, valid = 0 , invalid = 0 , unknown = 0 , known = 0;
void main() {
string cardnum[300];
int ccLen;
ifstream openfile("Sample Credit Card numbers.txt");
if (openfile.is_open())
{
while(!openfile.eof())
{
for (int count = 0; !openfile.eof(); count++)
{
getline(openfile,cardnum[count]);
ccLen = cardnum[count].length();
cout<<"sdfsd";
proc++;
if (ccLen !=0)
{
validateCC(cardnum[count]);
}
}
}
cout<<valid<<" validn";
cout<<invalid<<" invalidn";
cout<<unknown<< " unknwonn";
cout<<proc<<" processedn";
system("Pause");
}
}
void validateCC(string ccn) {
string msg;
bool OK;
int ccLen;
ccLen = ccn.length();
msg = checkCC(ccn, ccLen, OK);
if(!OK)
{
cout <<ccn<< msg << "n";
invalid++;
}
else
{
if(validateCCNum(ccn))
{
msg = setCCType(ccn);
setCCType(ccn);
valid++;
cout<<ccn<<msg << "Card Typen";
}
else
{
cout << (ccn)<<" Invalid"<< " credit card numbern";
invalid++;
}
}
}
string checkCC(string c, int cLen, bool& ccOK) {
string s = "";
ccOK = true;
for(int i=0;i<cLen && ccOK;++i)
ccOK = isdigit(c[i]);
if(ccOK == false) {
s = " Invalid credit card number digits";
} else if(cLen == 15) {
if(c.substr(0, 2) != "34" && c.substr(0, 2) != "37") {
ccOK = false;
s = " Invalid American Express credit card number";
}
} else if(cLen != 16) {
ccOK = false;
s = " Invalid credit card number length";
}
return s;
}
bool validateCCNum(string cc) {
bool flip = true;
int tmp, num = 0, ccLen = cc.length()-1;
for(int ndx=ccLen;ndx>=0;ndx--) {
if (flip)
num += cc[ndx] - '0';
else {
tmp = (cc[ndx] - '0') * 2;
if(tmp <= 9)
num += tmp;
else
num += (1 + (tmp - 10)); // max of 18
}
flip = !flip;
}
return num % 10 == 0;
}
string setCCType(string cc) {
int num = cc[0]-'0';
int num1 =cc[1]-'0';
int num2 = cc[2]-'0';
int num3 = cc[3]-'0';
string cct = " Unknown";
if(cc.length()==15 &&num ==3 &&num1 ==4|| cc.length()==15 &&num ==3 &&num1 ==7)
{
cct = " American Express";
known++;
}
else if(num == '4')
{
cct = " Visa";
known++;
}
else if(num ==5 && num1 ==1 ||num ==5 && num1 ==2|| num ==5 && num1 ==3||num ==5 && num1 ==4|| num ==5 && num1 ==5)
{
cct = " MasterCard";
known++;
}
else if (num == 6 && num1 ==0 && num2 == 1 && num3==1 || num ==6 && num==5)
{
cct = " Discover"; //ignoring other prefixes
known++;
}
else
{
unknown++;
}
return cct;
}
msg = setCCType(ccn);
setCCType(ccn);
你调用了函数两次。这将对known
/unknown
进行两次计数。