用于验证事务模块 (.cpp) 中 #define(d) 整数在 header 中提供的范围的条件语句

conditional statement to verify range provided by #define(d) integers in header for transaction module (.cpp)

第一次在 Whosebug 上发帖。我不确定问题是否足够清楚,所以我会尝试解释问题并提供我的代码。

我猜我的问题是一些与我在 header 中提到的成员函数定义中创建的条件 if 语句有关的问题。 header 具有#define 语句,这些语句具有整数变量的最小值和最大值,这些整数变量将从用户的输入中接受。我似乎无法根据提供的输入验证数字。我有一个 运行 测试,在条件语句之前和之后发出了检查值,无论输入什么,它们似乎都得到了验证。如果有任何不同,我也将所有条件都放在一个 if 语句中。

以下是一个简单会计应用程序的代码,它包含一个 header 和交易 (.cpp) 模块和 main.cpp:

// BTP200 研讨会 2:复合类型和隐私 MAIN.CPP

#include <iostream>
#include "AccountNumber.hpp"

using namespace std;
using namespace sict;

int main()
{
    AccountNumber myNumber;
    char name[41];
    int bankCode;
    int branchCode;
    int accNumber;

    cout << "Bank account app" << endl <<
    "===================" << endl << endl;
    cout << "Please enter your name: ";
    cin >> name;
    myNumber.setName(name);
    cout << "please enter your bank account, branch code" <<
    ", and account number as follows:" << endl << "999 999 99999: ";

    do
    {
        cin >> bankCode >> branchCode >> accNumber;
        myNumber.setAccountNumber(bankCode, branchCode, accNumber);
        myNumber.display();
    } while(!myNumber.isValid() && cout << "Invalid account number, (999 999 99999), try again: ");

    cout << "Thank you!" << endl;

    return 0;
}

ACCOUNTNUMBER.CPP

#include <iostream>
#include <cstring>
#include "AccountNumber.hpp"

#define MAX_NAME_LENGTH 40
#define MIN_BANKCODE    100
#define MAX_BANKCODE    999
#define MIN_BRANCHCODE  1
#define MAX_BRANCHCODE  220
#define MIN_ACCNO       10000
#define MAX_ACCNO       99999

using namespace std;

namespace sict
{
    void AccountNumber::setName(const char n[])
    {
        strcpy(name, n);
    };

    void AccountNumber::setAccountNumber(int bankc, int branchc, int an)
    {
        validAccNumber = false;

        int bankc_v=0, branchc_v=0, an_v=0;

        cout << bankc_v << branchc_v << an_v; //TESTING

        if (bankc >= MIN_BANKCODE || bankc <= MAX_BANKCODE)
        {
            bankCode = bankc;
            bankc_v=1;
        }
        else if (branchc >= MIN_BRANCHCODE || branchc <= MAX_BRANCHCODE)
        {
            branchCode = branchc;
            branchc_v=1;
        }
        else if (an_v >= MIN_ACCNO || an_v <= MAX_ACCNO)
        {
            accountNumber = an;
            an_v=1;
        }

        cout << bankc_v << branchc_v << an_v; //TESTING



        else if(bankc_v==1 && branchc_v==1 && an_v==1)
        {
            validAccNumber = true;
        }
    }

    void AccountNumber::display() const
    {
        if(isValid() == 1)
        {
            cout << "Name: " << name << ", Account number: " << bankCode << "-"
            << branchCode << "-" << accountNumber << endl;
        }
        else
        {
            cout << name << " does not have a valid account number." << endl;
        }
    }

    bool AccountNumber::isValid() const
    {
        return AccountNumber::validAccNumber;
    }   
}

ACCOUNTNUMBER.HPP

#ifndef AccountNumber_hpp
#define AccountNumber_hpp

#include <stdio.h>

#define MAX_NAME_LENGTH 40
#define MIN_BANKCODE    100
#define MAX_BANKCODE    999
#define MIN_BRANCHCODE  1
#define MAX_BRANCHCODE  220
#define MIN_ACCNO       10000
#define MAX_ACCNO       99999

namespace sict
{
    class AccountNumber
    {
        char name[MAX_NAME_LENGTH + 1];
        int  bankCode;
        int  branchCode;
        int  accountNumber;
        bool validAccNumber;

    public:
        void setName(const char n[]);
        void setAccountNumber(int bankc, int branchc, int  an);
        void display() const;
        bool isValid() const;
    };
}


#endif /* AccountNumber_hpp */

***已更新****

// BTP200 研讨会 2:复合类型和隐私 MAIN.CPP

#include <iostream>
#include "AccountNumber.hpp"

using namespace std;
using namespace sict;

int main()
{
    AccountNumber myNumber;
    char name[41];
    int bankCode;
    int branchCode;
    int accNumber;

    cout << "Bank account app" << endl <<
    "===================" << endl << endl;
    cout << "Please enter your name: ";
    cin >> name;
    myNumber.setName(name);
    cout << "please enter your bank account, branch code" <<
    ", and account number as follows:" << endl << "999 999 99999: ";

    do
    {
        cin >> bankCode >> branchCode >> accNumber;
        myNumber.setAccountNumber(bankCode, branchCode, accNumber);
        myNumber.display();
    } while(!myNumber.isValid() && cout << "Invalid account number, (999 999 99999), try again: ");

    cout << "Thank you!" << endl;

    return 0;
}

ACCOUNTNUMBER.CPP

#include <iostream>
#include <cstring>
#include "AccountNumber.hpp"

#define MAX_NAME_LENGTH 40
#define MIN_BANKCODE    100
#define MAX_BANKCODE    999
#define MIN_BRANCHCODE  1
#define MAX_BRANCHCODE  220
#define MIN_ACCNO       10000
#define MAX_ACCNO       99999

using namespace std;

namespace sict
{
    void AccountNumber::setName(const char n[])
    {
        strcpy(name, n);
    };

    void AccountNumber::setAccountNumber(int bankc, int branchc, int an)
{
    validAccNumber = false;

    int bankc_v=0, branchc_v=0, an_v=0;

    cout << bankc_v << branchc_v << an_v;

    if (bankc >= MIN_BANKCODE && bankc <= MAX_BANKCODE)
    {
        bankCode = bankc;
        bankc_v=1;
    }
    if (branchc >= MIN_BRANCHCODE && branchc <= MAX_BRANCHCODE)
    {
        branchCode = branchc;
        branchc_v=1;
    }
    if (an_v >= MIN_ACCNO && an_v <= MAX_ACCNO)
    {
        accountNumber = an;
        an_v=1;
    }

    cout << bankc_v << branchc_v << an_v;

    if(bankc_v==1 && branchc_v==1 && an_v==1)
    {
        validAccNumber = true;
    }
}


    void AccountNumber::display() const
    {
        if(isValid() == 1)
        {
            cout << "Name: " << name << ", Account number: " << bankCode << "-"
            << branchCode << "-" << accountNumber << endl;
        }
        else
        {
            cout << name << " does not have a valid account number." << endl;
        }
    }

    bool AccountNumber::isValid() const
    {
        return AccountNumber::validAccNumber;
    }   
}

ACCOUNTNUMBER.HPP

#ifndef AccountNumber_hpp
#define AccountNumber_hpp

#include <stdio.h>

#define MAX_NAME_LENGTH 40
#define MIN_BANKCODE    100
#define MAX_BANKCODE    999
#define MIN_BRANCHCODE  1
#define MAX_BRANCHCODE  220
#define MIN_ACCNO       10000
#define MAX_ACCNO       99999

namespace sict
{
    class AccountNumber
    {
        char name[MAX_NAME_LENGTH + 1];
        int  bankCode;
        int  branchCode;
        int  accountNumber;
        bool validAccNumber;

    public:
        void setName(const char n[]);
        void setAccountNumber(int bankc, int branchc, int  an);
        void display() const;
        bool isValid() const;
    };
}


#endif /* AccountNumber_hpp */

输出:

    Bank account app
===================

Please enter your name: John
please enter your bank account, branch code, and account number as follows:
999 999 99999: 1 123 12345
000010John does not have a valid account number.
Invalid account number, (999 999 99999), try again: 1234 123 12345
000010John does not have a valid account number.
Invalid account number, (999 999 99999), try again: 123 0 12345
000100John does not have a valid account number.
Invalid account number, (999 999 99999), try again: 123 1234 12345
000100John does not have a valid account number.
Invalid account number, (999 999 99999), try again: 123 123 123
000110John does not have a valid account number.
Invalid account number, (999 999 99999), try again: 123 123 123456
000110John does not have a valid account number.
Invalid account number, (999 999 99999), try again: 123 123 12345
000110John does not have a valid account number.
Invalid account number, (999 999 99999), try again: 123 12345
123
000100John does not have a valid account number.
Invalid account number, (999 999 99999), try again: 123 123 12345
000110John does not have a valid account number.
Invalid account number, (999 999 99999), try again: 123 123 
123
000110John does not have a valid account number.
Invalid account number, (999 999 99999), try again: 

您的 if 语句的计算结果始终为真。 例如,看看其中一个:

if (bankc >= MIN_BANKCODE || bankc <= MAX_BANKCODE)

在这个语句中,因为你使用的是'||'运算符,您基本上是在检查是否只有一个布尔子表达式为真。但是,要验证您的数字,您需要两个布尔子表达式都为真。

相反,您可能想要的是:

if (bankc >= MIN_BANKCODE && bankc <= MAX_BANKCODE)

使用“&&”确保您的两个布尔子表达式都将被检查。

这个

else if (an_v >= MIN_ACCNO || an_v <= MAX_ACCNO)

应该是

if (an >= MIN_ACCNO && an <= MAX_ACCNO)