0

Hello I've been working on a C++ banking application that should be able to hold more than one account with all the related field's. I have come across a few issues:

  • When displaying account info in Display or ShowInfo functions, the first letter of the first name and the middle initial wont show up.
  • When creating an account only the most recent account is able to be searched for and displayed in display data. I know that an array is needed for this to be possible, I'm just not sure if implemented this correctly.

Thanks for the help. Any input is appreciated!

#include <stdlib.h>
#include <conio.h>
#include <iostream>
#include <string>

using namespace std;

class BankAccount{
    double Balance = 0.0;
    char ans;
    public:struct Name{
        char Last_Name[50];
        char First_Name[50];
        char Middle_Initial[5];
    }Name;
    public:struct Account{
        char Type[1];
        int Account_Number;
    }Account;
    public:
    void CreateAccount();
    void Withdraw();
    void Deposit();
    void Display();
    void ShowInfo();
    int Menu();

};

void BankAccount::CreateAccount(){
    do
    {
        cout << "\nEnter account number: ";
        cin >> Account.Account_Number;
        cout << "\nEnter the last name for the account: ";
        cin.ignore();
        cin.getline(Name.Last_Name, 50);
        cout << "\nEnter the first name for the account: ";
        cin.ignore();
        cin.getline(Name.First_Name, 50);
        cout << "\nEnter the Middle initial for the account: ";
        cin.ignore();
        cin.getline(Name.Middle_Initial, 5);
        cout << "\nEnter the type of account (C/S) : ";
        cin >> Account.Type;
        cout << "\nEnter the initial balance of the account: ";
        cin >> Balance;
        cout << "\n\nAccount Created.";
        cout << "\n\nCreate new account? (Y/N) : ";
        cin >> ans;

        while (ans != 'Y' && ans != 'N'){
            cout << "Invalid input. Create new account? (Y/N) : ";
            cin >> ans;
        }
        cout << endl;
    } while (ans != 'N');
};
void BankAccount::Withdraw(){
    int actNum;
    double amount;
    cout << "Enter the account number for the account that you wish to withdraw funds: ";
    cin >> actNum;
    if (actNum == Account.Account_Number){
        cout << "Enter the amount you would like to withdraw: ";
        cin >> amount;
        Balance = Balance - amount;
    }
    else if (actNum != Account.Account_Number){
        cout << "No account found under that number! Try again!";
    }

}
void BankAccount::Deposit(){
    int actNum;
    double amount;
    cout << "Enter the account number for the account that you wish to deposit funds: ";
    cin >> actNum;
    if (actNum == Account.Account_Number){
        cout << "Enter the amount you would like to deposit: ";
        cin >> amount;
        Balance = Balance + amount;
    }
    else if (actNum != Account.Account_Number){
        cout << "No account found under that number! Try again!";
    }
}
void BankAccount::Display(){
    int actNum;
    cout << "Enter the account number for the account that you wish to display account information for: ";
    cin >> actNum;
    if (actNum == Account.Account_Number){
        cout << "Account details for " << Name.First_Name << " " << Name.Middle_Initial << " " << Name.Last_Name << "'s account: " << endl;
        cout << "Account Number: " << Account.Account_Number << endl;
        cout << "Account Type (Checking / Savings): " << Account.Type << endl;
        cout << "Account Balance:  $" << Balance << endl;
    }
    else if (actNum != Account.Account_Number){
        cout << "No account found under that number! Try again!";
    }

}
void BankAccount::ShowInfo(){
    cout << "Account details for " << Name.First_Name << " " << Name.Middle_Initial << " " << Name.Last_Name << "'s account: " << endl;
    cout << "Account Number: " << Account.Account_Number << endl;
    cout << "Account Type (Checking / Savings): " << Account.Type << endl;
    cout << "Account Balance:  $" << Balance << endl;    

}

int main(int argc, char *argv){
    BankAccount ob;
    char ch;

    cout << "Welcome to Console Banking Application V 1.0!";
    cout << "\nSelect an item from the list below by entering the corresponding letter.";
    do{
        cout << "\n\n A. Create Account \n B. Withdraw \n C. Deposit \n D. Show Account Details \n\n Q. Exit Application\n\n";
        ch = ob.Menu();
        switch (ch){
        case 'A':
        case 'a': ob.CreateAccount();
            ob.ShowInfo();
            break;
        case 'B': 
        case 'b': ob.Withdraw();
            break;
        case 'C':
        case 'c': ob.Deposit();
            break;
        case 'D':
        case 'd': ob.Display();
            break;
        case 'Q':
        case 'q': ob.ShowInfo();
                  exit(1);
            break;
        }

    } while (1);
}
int BankAccount::Menu(){
    char ch;
    cout << "Select an option: ";
    cin >> ch;
    return ch;
}
AddieCaddy
  • 21
  • 7
  • Why *wouldn't* you only be able to access one account? You read the account number, then you check if it's equal to `Account.Account_Number`, which is only one number. And if it's not equal, then you print the error message. – user253751 Feb 26 '16 at 03:48
  • 1
    Why using a char array ? [`std::string`](http://www.cplusplus.com/reference/string/string/) You include ``, why not using it ? Also `public` is not needed that often... – Blacktempel Feb 26 '16 at 03:49
  • Any tips on how i would go about using an array to hold the account info? – AddieCaddy Feb 27 '16 at 18:14

1 Answers1

1

The simple answer is: You only have one bank account.

If you look at your main function, you create one, and only one BankAccount. As you would probably guess, one BankAccount cannot be used to represent multiple BankAccounts(SoAs aside).

Therefore, you need an array of BankAccounts. It goes something like this:

BankAccount obs[10];

Now you have 10 BankAccounts, no more. So every time you want to create a new BankAccount, just make sure that you create it on a BankAccount in the array that is not currently in use.

obs[0].CreateAccount();
obs[0].ShowInfo();
obs[0].Deposit();
//Make a new account
obs[1].CreateAccount();
...

To go even further, let's consider the fact that you have 10 BankAccounts, and only 10. This is not very extensive now is it? What's a bank with only 10 accounts available? Probably out of business in due time.

The naive solution would be to just add more. 50, 100, even 1000. But do you really want to go back and update that number every single time? That's way too tedious.

Fortunately, there's a convenient container we can use:

#include <vector>
...
std::vector<BankAccount> obs;
...

This is basically an array that expands itself automatically when required. I will not go into detail on how to use vectors because I am sure that you can easily learn to do so yourself. I will, however, leave you with this link so you know where you can start.

Nard
  • 1,006
  • 7
  • 8