简体   繁体   中英

Why is my Bank Management System not working properly?

So, I am making this bank management system in c++ where I will have to give the user an option to create an account, deposit the money, withdraw it, and display the details. I also need to store in the array of objects so that the entire data can be displayed after the user exits. The restrictions are that I cannot use file handling. But it isn't working properly.

Please help.

When I run it, it keeps asking me for full name. How do I resolve this issue? I feel like this issue is occurring because of the persons array of type bankaccount, but I don't see any other possible way to do this. I have deleted the details of some functions because it became a lengthy block of code.

#include<iostream>
#include<string>
#include <time.h>  
#include <cstdlib>
using namespace std;
class bankaccount {
private:
    int id;
    string name;
    int cash;
    int money;
    int age;
public:
    string get_name() {
        return name;
    }
    int get_id() {
        return id;
    }
    void withdraw();
    void deposit();
    int see_money();
    bankaccount(int id1) {
        id = id1;

        cout << "\n Enter Full Name:";
        getline(cin, name);

    }
    friend ostream& operator<<(ostream& os, const bankaccount& d);




};

ostream& operator<<(ostream& os, bankaccount& d) {
    os << "\n Your name is:" << d.get_name();
    os << "\n Your id is:" << d.get_id();
    os << "\n You have a total of : " << d.see_money();
}
int main() {
    bankaccount persons[100] = 0;
    int option;
    int id;
    int number = 0;
    cout << "BANKING MANAGEMENT SYSTEM!" << endl;
    cout << "-------------------------------------------------------------------------------";
    while (1) {

        cout << "\nEnter 1 to create an account. Enter 2 to deposit money. Enter 3 to withdraw money. Enter 4 to check money. Enter 5 to display. Enter 6 to exit";
        cin >> option;
        switch (option) {
        case 1: {

            bankaccount p(number);
            persons[number] = p;

            cout << "Your ID is:" << number << endl;
            number++;
            break;
        }
        case 2: {
            cout << "\n Enter Your ID:";
            cin >> number;


            persons[number].deposit();
            break;
        }
        case 3: {
            cout << "\n Enter Your ID:";
            cin >> number;
            persons[number].withdraw();
            break;
        }
        case 4: {
            cout << "\n Enter Your ID:";
            cin >> number;
            persons[number].see_money();
            break;
        }
        case 5: {
            cout << "\n Enter Your ID:";
            cin >> number;
            cout << persons[number];
            break;
        }
        }
    }
}

这是输出:

bankaccount persons[100]=0;

Here you construct 100 objects of your bankaccount .

Your bankaccount constructor has these 2 lines:

cout<<"\n Enter Full Name:";
getline(cin,name);

So each time you create a bankaccount object, you're prompted for its name. You probably didn't intend that. You need to de-couple this, so asking the user for the Full Name, assigning it to a bankaccount, and constructing a bankaccount object are separate.

eg you can create option 6 to assign a name to the bankaccount instead of doing it inside the constructor of your bankaccount class.

If you aren't restricted the use of pointers or you don't have to specifically store it in a static array you can try this. You can create an array of bankaccount type pointers. In this case an array of 100 pointers. Each pointer will point to a separate object of bankaccount. https://imgur.com/a/KflNVbc for a better visual understanding.

You can create it like this:

bankaccount* persons[100];

So in case 1, when you create a new account and add it to the array you can do this:

persons[number] = new bankaccount();
number++;

This way you won't have to change your constructor. Just a cool way of doing this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM