0

I want the following C++ program of bank accounts to run for 5 customers. But it's saying that my object array has an incomplete type. This code works ok for 1 customer, but it cannot work for more than one customer.

    // bank account
    #include <iostream>
    using namespace std;
    

    class BA 
    {
        // BA C[2];
        string name;
        int acc_no;
        string acc_type;
        int balance;
    
    public:
        int getdata();
  
    
        void deposit()
        {
            int a;
            cout << "How much you want to deposit: ";
            cin >> a;
            balance += a;
        }
    
        void withdraw()
        {
            int w;
            cout << "How much you want to withdraw: ";
            cin >> w;
            balance -= w;
        }
    
        void display()
        {
            for (int i = 0; i < 10; i++)
            {
                cout << "Name: " << name << endl;
                cout << "Account no. : " << acc_no << endl;
                cout << "Account type: " << acc_type << endl;
                cout << "Your current balance: " << balance << endl;
            }
        };
    
        BA C[2];
        int BA ::getdata()
        {
            for (int i = 0; i < 2; i++)
            {
                cout << "Enter your name: " << i;
                cin >> C[i].name;
                cout << "Enter your acc_no: " << i;
                cin >> C[i].acc_no;
                cout << "Enter your acc_type: " << i;
                cin >> C[i].acc_type;
                cout << "Enter your balance: " << i;
                cin >> C[i].balance;
            }
        }
    
        int main()
        {
    
            BA C[2];
            int x;
            // for(int i=0;i<10;i++){
            do
            {
                cout << endl
                     << "To enter your Bank Information press 1: " << endl;
                cout << "To deposit an amount in your bank press 2: " << endl;
                cout << "To withdraw an amount from your bank press 3:" << endl;
                cout << "To view your current balance and bank info press 4:" << endl;
                cout << "To quit press 5:" << endl;
                cout << "\n\twhat is your option: ";
                cin >> x;
    
                switch (x)
                // for(int i=0;i<10;i++){
                {
                case 1:
                    for (int i = 0; i < 2; i++)
                    {
                        C[i].getdata();
                    }
                    break;
    
                case 2:
                    for (int i = 0; i < 2; i++)
                        C[i].deposit();
                    break;
    
                case 3:
                    for (int i = 0; i < 2; i++)
                        C[i].withdraw();
                    break;
    
                case 4:
                    for (int i = 0; i < 2; i++)
                        C[i].display();
                    break;
    
                case 5:
                    break;
    
                default:
                    cout << "Error input try again:" << endl;
                }
            } while (x != 5);
            return 0;
        }
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Sorry for writing same sentences again and again at start cause I was unable to post – Aditya Kumdale Apr 29 '22 at 17:36
  • 4
    An object of type `BA` cannot hold another object of type `BA`, because that would mean infinite objects. Why do you want an array of `BA` in `BA`? – Yksisarvinen Apr 29 '22 at 17:37
  • `BA` represents a *single* account. There is no good reason (nor is it possible) for `BA` to hold an array of other `BA` objects (it can, however, hold an array of *pointers* to `BA` objects, if you really need it to). Just as it doesn't make sense for `BA::display()` to output the account details in a loop, or for `BA::getdata()` to read in the account details in a loop. – Remy Lebeau Apr 29 '22 at 17:39
  • You have BA C[2] within the BA class definition on line 45. What did you mean by that? – fukanchik Apr 29 '22 at 17:40
  • You forgot to end the declaration of `BA`. In the current code `C`, the non-inline implementation of `BA::getdata()` and `main` are positioned inside the class body of `BA`. At least for the former 2 this is an error: An object must not be part of itself and non-inline member function definitions need to be positioned outside of the class body. – fabian Apr 29 '22 at 18:09
  • 1
    Side note: What you ran up against was the code to description ratio test. The Stack Overflow server makes a fairly logical assumption that if you have a problem that requires a lot of code to reproduce, that problem must be large or very complicated and also require a lot of descriptive text. Usually the best solution id a problem doesn't require a lot of description is not to insert garbage description, but to reduce the amount of code needed to reproduce the problem with a [mre]. The beauty of the [mre] is making one usually reduces the noise around the problem, making it easier to spot. – user4581301 Apr 29 '22 at 18:36
  • In BA C[2], C[2] is an object array. I don't want to create object BA in BA.I just want the code to run for 2 customers rather than 1.Please remove my messed up code lines if needed and update it and make it working for 2 customers.I am beginner to oops c++ you see. – Aditya Kumdale Apr 30 '22 at 19:05

0 Answers0