第一次在stackoverflow上发帖。我不确定问题是否足够清楚,所以我将尝试解释问题并提供我的代码。
我猜我的问题与我在标题中提到的成员函数的定义中创建的条件 if 语句有关。标头具有#define 语句,这些语句具有从用户输入接受的整数变量的 MIN 和 MAX 整数值。我似乎无法从提供的输入中验证数字。我已经用条件语句之前和之后发出的校验值运行了一个测试,无论输入什么,它们似乎都得到了验证。如果这有什么不同,我在一个 if 语句中也有所有条件。
以下是一个简单会计应用程序的代码,它包含一个标头和事务(.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;
}
帐号.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;
}
}
帐号.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: