-2

我不确定为什么变量的代码是已知的。它总是加倍 2 倍。该代码假设显示处理有效卡无效卡和未知卡的时间。已知的卡是美国运通发现签证和大师的卡。我试图计算它们的数量,但似乎由于某种原因它们总是加倍

#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<<" valid\n";
        cout<<invalid<<" invalid\n";
        cout<<unknown<< " unknwon\n";
        cout<<proc<<" processed\n";
        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 Type\n"; 
        }
        else
        {

        cout << (ccn)<<" Invalid"<< " credit card number\n";
        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;
}
4

1 回答 1

2
 msg =  setCCType(ccn);
        setCCType(ccn);

您正在调用该函数两次。这计算known/unknown两次。

于 2013-01-19T15:06:11.917 回答