1

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++;
        }
4
  • Your code snippet is incomplete. You should edit it to include all the relevant code and edit your question to use some better grammar if you can. Just looking at what you have now, I can tell something very bad is happening with EnterAccountDetails() as you are having to apply some ugly fix afterwards. Commented Sep 17, 2016 at 17:56
  • @CJMki can we see your EntrAccountDetails() function code? Commented Sep 17, 2016 at 17:59
  • @MichaelMcGuire: Hey really sorry for the inconvenient. i am new to this forum. Commented Sep 17, 2016 at 18:23
  • You haven't asked anything. Commented Sep 17, 2016 at 18:29

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

4 Comments

@ Guilaume, To use find do we have to include any lib packages ?
No. You'll just have to include a header from the standard library. This library is linked to your program by default.
Thanks and what does auto do there ?
auto will deduce the type of the variable by checking the expression following the =
1

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 ...

7 Comments

Go ahead and add the (now directly below this comment) function definition to your question. It appears to be ok but hard to read in a comment. Then, what is your specific question?
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) { return BankAccount; } else
i have been trying to figure out how to put a code into the comments :S , however the bug is , if the user enters 0 to the BankAccount.accNo to sould return a null value to the array and limit the array size upto that point .
Set BankAccount.accNo to NULL and then return the BankAccount object. See edited answer.
I don't see the difference between NULL and 0
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.