I want to return something which tells me that the account number and CNIC do not match with any customer id
Well there are two ways to consider this problem:
if your Search()
shall always have valid arguments (i.e. existing account number and CNIC), and never have wrong values, it shall fail loudly when it has a wrong set of arguments by throwing an exception:
AccountHolder Search(int accno, string CNIC, AccountHolder* customer, int counter) {
for (int i = 0;i < counter;i++) {
if (account == customer[i].getAccno() && CNIC == customer[i].getCNIC()) {
return customer[i];
}
}
throw std::invalid_argument("Unknown account");
}
of course, a custom designed exception is even better.
but if that situation is part of your design and this is not an error, but a feature, then it shall return a value that you can test against to tell you that the customer does not exist:
So you shall design your AccountHolder
class with a custom field that you can check so you can create a global unknownAccountHolder
instance that you return from this function and then can check against. As you do not give your AccountHolder
design.
class AccountHolder {
static const AccountHolder unknownAccountHolder(-1, "invalid");
[…]
}
AccountHolder Search(int accno, string CNIC, AccountHolder* customer, int counter) {
for (int i = 0;i < counter;i++) {
if (account == customer[i].getAccno() && CNIC == customer[i].getCNIC()) {
return customer[i];
}
}
return AccountHolder::unknownAccountHolder;
}
- use C style error handling is a wrong third option
you may use other options suggested to you: using a returned pointer, passing a boolean as parameter, returning a boolean and taking the object as argument… But that's all c-style coding and bad design as it's complicating your Search()
function prototype and/or adding more memory management than necessary. You're doing C++ here, so try to keep it simple and use good OOP practice.