-1

My data structure is:

vector <pair <int, vector <SavingsAccount*>>> accVec;

where the int is the bank account number.

My goal is to check if the account number input by the user matches any int within any pair in the accVec vector.

In main, I'm using something like:

do {
    iter = find_if (accVec.begin(), accVec.end(), findAccID);

    if (iter == accVec.end()) {
        cout << endl << "ERROR: Account Does Not Exist. Try Again." << endl;
    }
} while (iter == accVec.end());

Function:

bool findAccID(pair <int, vector <SavingsAccount*>> accPair) {
    static int i = 0, accID;

    if (i == 0) {
        cout << endl << "Enter The Account Number In Which You Want To Deposit: ";
        cin >> accID;
        i++;
    }

    if (accID == accPair.first) {
        return true;
    }
    return false;
}

I am asking the user for the account number inside the function because I could not find a way to pass the accID as a parameter from main(). I want the question to repeat as long as the user types the wrong account number.

Any suggestions?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
ionics
  • 17
  • 2
  • The best suggestion is to read the chapter in your C++ book that shows an example of using `std::find_if` with a lambda, and a description of standard library algorithms, in general. The shown code uses the comparator parameter in fundamentally wrong ways. You should not be prompting for user input in the comparator! Prompt for it first, then call `find_if`, then figure out, based on the results, whether the input is valid, or not. – Sam Varshavchik Feb 26 '19 at 00:13
  • 1
    Why does your `vector` hold raw pointer to `SavingsAccount` instead of actual objects? Also, why are there multiple accounts that go by the same ID? – Swordfish Feb 26 '19 at 00:29
  • @Swordfish a bank customer may have a main account ID with multiple subaccounts (savings, checking, loans, etc) – Remy Lebeau Feb 26 '19 at 03:22
  • 1
    @RemyLebeau And those subaccounts have subaccount-ID that combined with the account-ID identifies them? What is that subaccount-ID? The index where it can be found in the vector? What if a subaccount gets removed? All subaccounts with a higher ID (index) change their ID? Nah, that design is flawed. – Swordfish Feb 26 '19 at 04:44
  • Yeah it’s just a class assignment where the user can have as many sub savings accounts as he wants. The sub account id is the index but as far as the assignment goes, there shouldn’t be an option to delete sub accounts but to rather deposit more money into a particular sub account of a particular account/account ID – ionics Feb 26 '19 at 12:39
  • @Swordfish in my real-world banks, I have a main ID that identifies me as a customer, and my sub-accounts each have a short ID that gets appended to that main ID for routing purposes, eg `123456789-S01`, `123456789-S02`, etc. In the OP's example, the `int` in the `pair` could be the main ID, and `SavingsAccount` could have a `string` member for the sub ID. – Remy Lebeau Feb 26 '19 at 16:10
  • @RemyLebeau since he calls the first member of the pair `accID` there is no id in `SavingsAccount`. – Swordfish Feb 26 '19 at 16:25
  • @Swordfish I see no design issue having a vector of sub-accounts identified by a main `int`, or using pointers (eg, if the sub-accounts were polymorphic classes). Using RAW pointers is an issue, though, prefer smart pointers instead. I do see a design issue using an index as a sub-account ID, but that is mitigated by this being a simple class assignment. Adding a sub-account ID member would be trivial. – Remy Lebeau Feb 26 '19 at 16:32

1 Answers1

2

Use a lambda as predicate for std::find_if():

#include <algorithm>
#include <utility>
#include <vector>
#include <iostream>

using namespace std;

struct SavingsAccount{};

int main()
{
    vector<pair<int, vector<SavingsAccount>>> accVec;
    vector<pair<int, vector<SavingsAccount>>>::iterator iter;

    do {
        cout << "Enter The Account Number In Which You Want To Deposit: ";
        int accID;
        cin >> accID;

        iter = find_if(accVec.begin(), accVec.end(),
                       [=](pair<int, vector<SavingsAccount>> const &accPair) {
                           return accPair.first == accID;
                       }
        );

        if (iter == accVec.end()) {
            cout << endl << "ERROR: Account Does Not Exist. Try Again." << endl;
        }
    } while (iter == accVec.end());
}

or a functor:

#include <algorithm>
#include <utility>
#include <vector>
#include <iostream>

using namespace std;

struct SavingsAccount{};

class AccountFinder
{
    int accID;

public:
    AccountFinder(int accID) : accID{ accID } {};

    bool operator()(pair<int, vector<SavingsAccount>> const &accPair)
    {
        return accPair.first == accID;
    }
};

int main()
{
    vector<pair<int, vector<SavingsAccount>>> accVec;
    vector<pair<int, vector<SavingsAccount>>>::iterator iter;

    do {
        cout << "Enter The Account Number In Which You Want To Deposit: ";
        int accID;
        cin >> accID;

        iter = find_if(accVec.begin(), accVec.end(), AccountFinder(accID));

        if (iter == accVec.end()) {
            cout << endl << "ERROR: Account Does Not Exist. Try Again." << endl;
        }
    } while (iter == accVec.end());
}
Swordfish
  • 12,971
  • 3
  • 21
  • 43