if / else和参数放置C ++

时间:2013-01-13 22:34:07

标签: c++ if-statement

我最近开始使用C ++并尝试理解语法,但我遇到了放置问题。我必须进行信用卡验证功能,要求提供有效的信用卡,然后我必须打印出信用卡类型。

我唯一的问题是确定信用卡类型的if语句。我不知道究竟在哪里放置它们以及我是否缺少任何括号等。

对我来说问题的确切块是:

        if(c.substr(0, 2) == "65" || c.substr(0, 5) == "6011" || c.substr(0, 7) >= "622126" && c.substr(0, 7) <= "622925" || c.substr(0, 4) >= "644" && c.substr(0, 4) <= "649")
            s = "You have a Discover card";
        if(c.substr(0, 2) >= "51" && c.substr(0, 2) <= "55")
            s = "You have a MasterCard card";
        if(c.substr(0, 1) == "4")
            s = "You have a Visa card";
        if(c.substr(0, 2) == "34" || c.substr(0, 2) == "37")
            s = "You have an American Express card"; 

这是我的代码。

#include <iostream>
#include <string>
using namespace std;

void validateCC();
string checkCC(string, int, bool&);
bool validateCCNum(string);

void main() {
    char again;
    cout << "Validate a credit card number (Y/N)? ";
    cin >> again;
    while (toupper(again) == 'Y') {
        validateCC();
        cout << "Validate a credit card number (Y/N)? ";
        cin >> again;
    }
}

void validateCC() {
    string ccn, msg;
    bool OK;
    int ccLen;
    cout << "Please enter a 15 or 16 digit credit card number."
        << "\n(No spaces or hyphens): ";
    cin >> ccn;
    ccLen = ccn.length();
    msg = checkCC(ccn, ccLen, OK);
    if(!OK)
        cout << msg;
    else
        if(validateCCNum(ccn))
            cout << "Valid credit card number\n";
        else
            cout << "Invalid credit card number\n";
    cout << "\n" << endl;
}

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(c.substr(0, 2) == "65" || c.substr(0, 5) == "6011" || c.substr(0, 7) >= "622126" && c.substr(0, 7) <= "622925" || c.substr(0, 4) >= "644" && c.substr(0, 4) <= "649")
            s = "You have a Discover card";
        if(c.substr(0, 2) >= "51" && c.substr(0, 2) <= "55")
            s = "You have a MasterCard card";
        if(c.substr(0, 1) == "4")
            s = "You have a Visa card";
        if(c.substr(0, 2) == "34" || c.substr(0, 2) == "37")
            s = "You have an American Express card"; 

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

我不知道有问题的块是属于它当前的位置,还是我将它放在validateCC()部分中,然后为它添加一个参数。

谢谢。

4 个答案:

答案 0 :(得分:1)

尝试将子字符串更改为int(stringVar&gt;&gt; IntVar;),以便您可以评估&lt; =&gt; = etc ...

    #include <iostream>
#include <string>
using namespace std;

void validateCC();
string checkCC(string, int, bool&);
bool validateCCNum(string);

void main() {
    char again;
    cout << "Validate a credit card number (Y/N)? ";
    cin >> again;
    while (toupper(again) == 'Y') {
        validateCC();
        cout << "Validate a credit card number (Y/N)? ";
        cin >> again;
    }
}
string checkCardType(string c){
if(atoi(c.substr(0, 2).c_str()) == 65 || atoi(c.substr(0, 5).c_str()) == 6011 || atoi(c.substr(0, 7).c_str()) >= 622126 && atoi(c.substr(0, 7).c_str()) <= 622925 || atoi(c.substr(0, 4).c_str()) >= 644 && atoi(c.substr(0, 4).c_str()) <= 649)
            return "You have a Discover card";
        if(atoi(c.substr(0, 2).c_str()) >= 51 && atoi(c.substr(0, 2).c_str()) <= 55)
            return "You have a MasterCard card";
        if(atoi(c.substr(0, 1).c_str()) == 4)
            return "You have a Visa card";
        if(atoi(c.substr(0, 2).c_str()) == 34 || atoi(c.substr(0, 2).c_str()) == 37)
            return "You have an American Express card"; 
        return "Card not recognized";
}
void validateCC() {
    string ccn, msg;
    bool OK;
    int ccLen;
    cout << "Please enter a 15 or 16 digit credit card number."
        << "\n(No spaces or hyphens): ";
    cin >> ccn;
    ccLen = ccn.length();
    msg = checkCC(ccn, ccLen, OK);
    if(!OK)
        cout << msg;
    else
        if(validateCCNum(ccn)){
            cout << "Valid credit card number\n";
            cout << checkCardType(ccn);}
        else
            cout << "Invalid credit card number\n";
    cout << "\n" << endl;
}

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

答案 1 :(得分:0)

我不确定这是否有效:

c.substr(0,2)==“34”

为什么不直接将String解析为整数(atoi),然后将其与'=='进行比较。字符串也会与“string”.compare(“string2”)(如果它的字符串相同则返回0)进行比较。

答案 2 :(得分:0)

虽然不是最有效的方式,但却是唯一的 我在你的代码中看到的问题是你花了太多钱 某些情况下的字符:c.substr(0, 5) == "6011"可以 只有在c.size() == 4时才会成真。

为了它的价值,我会将前缀声明为static std::string const并在其上使用std::equal(首先 检查了这个数字是否足够长)。或者, 我在第一个字符上使用了一个开关:

switch (c[0])
{
case '6':
    //  Additional tests for Discover...
    break;
case '5':
    //  Additional tests for MasterCard...
    break;

case '4':
    //  Additional tests for Visa...
    break;

case '3':
    //  Additional tests for American express..
    break;
}

或者甚至更可能的是,我会使用正则表达式 从配置文件中读取的模式;这是不是的类型 您想要硬编码到程序中的信息。

答案 3 :(得分:0)

std::string::substr方法返回一个字符串,该字符串可以是compared到另一个字符串。到目前为止,您的代码看起来很好。

要使用括号,请查看C++ Operator Precedence。据我了解代码,您在if条件中不需要括号。但如果您有疑问,请使用括号来明确您的意图。

您无需传递信用卡号码的长度,因为checkCC可以自行确定。同时将信用卡号码作为

传递
string checkCC(const string &c, ...)

因为这可以避免字符串的副本。