Imagine there is an array of account objects
[
{"account_number":1,"customer_name":"John"},
{"account_number":2,"customer_name":"Sally"}
]
I want to write a method that takes in an account number and returns the matching account object. I initially came up with this
Account AccountDb::get_account(int account_number) {
for (auto &account_element : account_list_) {
Account account = account_element;
bool account_found = (account.account_number() == account_number);
if (account_found) {
return account;
}
}
// no matching Account
return ???;
}
But I'm not sure that's a good way to do it especially since it wouldn't work if there was no matching account. Any ideas how to tackle this?
I'm pretty new to c++ and I'm using nlohmann-json for my json parsing. Any help would be appreciated. Thanks!