0

i am new to c++ and currently faces some obstacles and hope that someone can help me to solve this problem. And below is my code :

At first, the user need to enter "account number" , "account holder name" and "deposit" and all the data will store into a text file which will automatically create. Then the user need to login with the correct "account number" and "account holder name" . But after login successfully, it will display all the accounts which the user created just now.

title of program : Bank Management System

Problem : - if create more than one account, then it will display all the accounts that have created at one time.

#include <iostream>
#include <stdlib.h>
#include <fstream>
using namespace std ;

void create_account ()
{   long long int acc_no ;
    string name ;
    double deposit ;
    char choice ;

    do{
        system ("cls");
        cout << "PLEASE CREATE YOUR ACCOUNT !" <<   endl ;
        cout << "ENTER THE ACCOUNT NO : " ;
        cin >> acc_no ;
        cout <<"ENTER THE NAME OF THE ACCOUNT HOLDER : " ;
        cin.ignore() ;
        getline( cin, name ) ;
        cout << "ENTER AMOUNT FOR DEPOSIT : RM " ;
        cin  >> deposit ;

        ofstream outfile ("accounts.txt", ios::app) ;
        outfile << endl << acc_no << " " << name << " " << deposit  << endl ;
        outfile.close() ;
        cout << endl << endl ;
        cout << "ACCOUNT SUCCESSFULLY CREATED !" << endl << endl ;
        system ("pause") ;
    do{
           cout << "DO YOU WANT TO CONTINUE ? "<< endl ;
           cout << "ENTER YOUR CHOICE ( Y OR N ): " ;
           cin >> choice ;
       }while (choice != 'y' && choice != 'Y' && choice != 'n' && choice != 'N' );
    }while (choice == 'y' || choice == 'Y');
}


void view_account ()
{
   ifstream infile("accounts.txt",ios :: in ) ;
    string x , y , z ;
    while (infile >> x >> y >> z )
    {
        cout << endl <<"***************************************************"
             << endl << "ACCOUNT NUMBER IS : " << x
             << endl << "ACCOUNT HOLDER NAME IS : " << y
             << endl << "BALANCE AMOUNT : RM " << z  << endl
             << endl <<"***************************************************" ;
    }
    infile.close() ;
    cout << endl << endl ;
    system("pause");
}



void login()
{   string acc_no1, name1;

    int offset ;
    string line ;
    ifstream infile ;
    infile.open("accounts.txt");
        cout << endl << endl ;
        cout << "PLEASE LOGIN YOUR ACCOUNT ! " << endl ;
        cout << "ENTER YOU ACCOUNT NO : " ;
        cin >> acc_no1 ;
        cout << "ENTER ACCOUNT HOLDER NAME : ";
        cin.ignore() ;
        getline ( cin , name1 ) ;
        if (infile.is_open())
        {
            while(!infile.eof())
            {
                getline (infile,line);
                if ( (offset = line.find(acc_no1,0)) != string::npos && ((offset = line.find(name1,0)) != string::npos) )
                {
                    cout << "LOGIN SUCCESSFULLY" << endl ;
                    system("pause");
                    view_account ();
                }
            }
            infile.close() ;
        }else
        cout << "" ;
        system("cls") ;
        cout << endl << "LOGIN FAILED !" << endl ;
        system("pause");

}

int main ()
{
    create_account () ;
    login();
}
derk
  • 43
  • 3
  • 3
    Welcomat stackoverflow! Please note that your source code is very long and you are asking multiple different questions at once. Please avoid this (it may cause frustrating downvotes and no answers). It is better to provide a **minimal** reproducible example and one question so that we as experts don't have to spent too much time to find and answer. Also: The question headline should reflect your question, not the what your example code is doing. Nevertheless: Interesting source code :-) – R Yoda Sep 16 '19 at 05:50
  • read [mcve] and http://sscce.org/ to know how to create one – phuclv Sep 16 '19 at 07:07

1 Answers1

1

Problem : - if create more than one account, then it will display all the accounts that have created at one time.

That is because your view_account() function loops over all accounts in the accounts.txt file and prints all of them. And not just the one you did just login to. You can pass and compare the account number and print only the matching accounts to fix this for example.


You code could use some abstractions. I'd suggest you create a class account and overload the bitshift operators for plugging them to the IO streams interface.

Then you can write an account_store class, that can load and store accounts from file and hold the accounts in memory in between.

You can add login methods to both of these. account::login() would verify the account name, account_store::login_account() would query for the account number and fetch the matching account object from the container (if actually present) and then attempt to login the user.

The remaining control flow should be so simple, it should be trivial to make sure that only the relevant account's details are displayed.

moooeeeep
  • 31,622
  • 22
  • 98
  • 187