Charith Jayawardana
Charith Jayawardana

Reputation: 105

How to check if object array has the referred object

for my school project i am suppose to make a banking system. It promts user to create as account objects up to ten and then they are inserted into an object array. and later i need to refer to certain object in that array to produce a forecast using referred object , so far this is what i have done

            cout << "\nHow many accounts do you wish to crate: \n";
        cin >> accounts;
        for (int i = 0; i < accounts; i++)
        {
            cout << "\n>> Enter your details for your  Account: " << accCount << " <<" << endl;
            newAccounts[i] = EnterAccountDetails();
            if (newAccounts[i].accNo == 0)
            {
                for (int j = i; j < accounts; j++)
                {
                    newAccounts[j] = newAccounts[j + 1];
                    accounts-=1;
                }
                break;
            }
            accCount++;
        }

Upvotes: 1

Views: 83

Answers (2)

RigidBody
RigidBody

Reputation: 664

The above looks pretty good other than what if I enter >10 on the cin >> accounts call. (you need to limit what I can enter). What you end up with, presumably, since we can't see your EnterAccountDetails() function, is an array of objects. You can iterate this object array via native index numbers, and using . notation, the individual properties.

for(int i=0, i < accCount, ++i) {
   if (newAccounts[i].someProperty == someValue) {
      dostuff();
   }
}  

Customer EnterAccountDetails() {
    Customer BankAccount;
    cout << "\n>Enter Bank Account Number (4-digit):\n";
    cout << ">If you wish to stop creating new accounts press 0 .." << endl;
    cin >> BankAccount.accNo;
    if (BankAccount.accNo == 0) {
        BankAccount.accNo = NULL;
        return BankAccount;
     } else ...

Upvotes: 1

Guillaume Racicot
Guillaume Racicot

Reputation: 41780

You could use std::find, which is a function that do just that!

If your objects don't have operator== defined, you can use std::find_if with a lambda instead.

// If you have operator== defined
auto element = std::find(std::begin(yourArray), std::end(yourArray), elementToFind);

if (element != std::end(yourArray)) {
    // Your elementToFind exist in the array!
    // You can access it with `*element`
}

With std::find_if:

// If don't have operator== defined
auto element = std::find_if(std::begin(yourArray), std::end(yourArray), [&](auto&& check){
    return elementToFind.someProperty == check.someProperty;
});

if (element != std::end(yourArray)) {
    // Your elementToFind exist in the array!
    // You can access it with `*element`
}

The syntax [](){ /* return ... */ } is called a lambda expression, it's like a inline function of some sort that you send to the std::find_if function so it can compare elements for equality.

Please note that you'll have to include the following header:

#include <algorithm>

You can check more about about the algorithm library at Algorithms library

Upvotes: 1

Related Questions