0

This exercise that i'm trying to do reads something like this at one point:

1 - Generate a unique account number for each depositor For the first depositor, account number will be BA1000, for the second depositor it will be BA1001 and so on.

2 - The number of depositors will be entered by user from keyboard.

So, i know the code below is not enough, but i just cannot understand how am i supposed to know the number of objects(depositors) before the user enters them, so i can use that number inside my generateAccount function, so i can know for how long to loop through depositors to get that "BA1000" style bank account.

#include <iostream>

using namespace std;

class BankAccount {

public:

string depistor_name;
string depositor_address;
string account_type;
int account_balance, nr_transactions;

void generateAccount () {
    string acc = "BA";
    int acc_nr = 1000;

    for ( int i = 0; i < //unknown number of objects so i have no variable to put here ; i++) {

    // below are comments of what i would need to do with this function
    // iterate and add +i to the acc_nr variable, until theres no more depositors.
   // print "BA" string + new acc_nr variable for each object
}

};

int main(int argc, const char * argv[]) {

return 0;

}

If i wouldn't need to use the number of objects(depositors) that the user would enter from keyboard, i would normally just create an array of objects like so:

int x;
std::cout << "Please enter number of depositors";
cin >> x;
BankAccount ac[x];

But i need to know how many depositors the user is going to enter so i can iterate and generate the account as shown above and i dont know if theres a way to pull x from main inside my function thats inside my class.

Please ask for more details if you don't understand what i'm trying to say as this may be confusing.

Allen M
  • 73
  • 6
  • 1
    Did you consider using [`std::vector`](http://en.cppreference.com/w/cpp/container/vector)? – Algirdas Preidžius Mar 10 '17 at 13:09
  • `BankAccount ac[x];` is not standard C++ when `x` is not a constant expression, and this should be avoided. Prefer using a `std::vector` where possible, otherwise you would need to allocate memory dynamically. – crashmstr Mar 10 '17 at 13:10
  • Thats not really what i asked for, but i'll take that advice. I still don't know how am i supposed to do the generateAccount function without knowing how many objects the user is going to enter from keyboard. – Allen M Mar 10 '17 at 13:16
  • Check [my answer on this related question](http://stackoverflow.com/a/42675678/3316645) - its Java, but the approach is the same. And *do* use a `std::vector`. – domsson Mar 10 '17 at 13:31

0 Answers0