不确定为什么代码计数两次

时间:2013-01-19 15:02:26

标签: c++ counter

我不知道为什么变量的代码已知。它总是翻倍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;
}

1 个答案:

答案 0 :(得分:2)

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

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