0

strong text

As I am trying to code a program for ATM SYSTEM ,I am facing a problem with the array of objects for values/data getting overridden for all the objects by the last object data/values means, the values of the first and middle objects are getting replaced by the values or data of the last object as i go on accepting values for the next objects in the array i am trying, to use the array of objects in the program to make my code get as minimized as possible.
Please Help me here, How to solve the point in the code. I will Solve it again with your best Suggestion.

#include <iostream>
#include <string>
#include <vector>
using namespace std;

string Card_no,Account_no,Person_name;
    long int Pin_no,num;

class Login
{
private :


public :

    void setPerson_name(string D)
    {
        Person_name = D;
    }
    void setAccount_no(string C)
    {
        Account_no = C;
    }
    void setCard_no(string A)
    {
        Card_no = A;
    }
    void setPin_no(long B)
    {
        Pin_no = B;
    }


    string getPerson_name()
    {
        return Person_name;
    }
    string getAccount_no()
    {
        return Account_no;
    }
    string getCard_no()
    {
        return Card_no;
    }
    int getPin_no()
    {
        return Pin_no;
    }

        void accept_customer_details()
        {
            string person_name,account_number,card_number;
            long int pin_number;

            cout<<"\n Accepting Employee Details : \n"<<endl;
            cout<<"Enter the Person Name : "<<endl;
            cin>>person_name;
            cout<<"Enter the Account Number of the Person : "<<endl;
            cin>>account_number;
            cout<<"Enter the 16 digit ATM Card Number of the Person : "<<endl;
            cin>>card_number;
            cout<<"Enter the 4 digit ATM PIN NUMBER : "<<endl;
            cin>>pin_number;

            setPerson_name(person_name);
            setAccount_no(account_number);
            setCard_no(card_number);
            setPin_no(pin_number);

        }

        void display_customer_details()
        {
            string pnm,an,cn;
            int pn;

            pnm = getPerson_name();
            an = getAccount_no();
            cn  = getCard_no();
            pn = getPin_no();

        cout<<"\n Displaying the Employee details : \n"<<endl;
        cout<<"The Person's Name : "<<pnm<<endl;
        cout<<"The Account Number of the Person : "<<an<<endl;
        cout<<"The 16 digit ATM Card Number of the Person : "<<cn<<endl;
        cout<<"The 4 digit ATM PIN NUMBER : "<<pn<<endl;

        }


};

class Access : public Login
{

public :

    void account_ass(vector <Login> &VL,int sz)
        {
            num = sz;
            Login l[num];
            string ucn,upn;
            //string CARD_NUMBER[sz],PIN_NUMBER[sz],ACC_NO[sz],P_NAME[sz];
            int flag0 = 0,flag1 = 0,flag2 = 0,flag02 = 0;
            int chance = 3,c = 0,p = 0;

            for(int o = 0;o<num;o++)
            {
                cout<<"value of o is : "<<o<<endl;
                l[o] = VL[o];


cout<<"\n CARD_NUMBER IS HERE : "<<l[o].getCard_no()<<"\n AND ITS PIN NUMBER IS : "<<l[o].getPin_no()<<endl;
cout<<"The Account Number is : "<<l[o].getAccount_no()<<" \n Person's Name is : "<<l[o].getPerson_name()<<endl;
                    cout<<"Object :"<<o+1<<" address : "<<&l[o]<<endl;
            }


       }
};
int main()
{
    int N;
    cout<<"Enter how many person's details you want to add :- "<<endl;
    cin>>N;

    Login lg[N];
    //Access ac[N];
    vector <Access> ac;
    Access a;
    vector <Login> vl;

    for(int K = 0;K<N;K++)
    {
        ac[K].accept_customer_details();

    }
     for(int I=0;I<N;I++)
     {
         ac[I].display_customer_details();
        vl.push_back(ac[I]);
     }
    a.account_ass(vl,N);

    return 0;
}
  • 1
    `Login l[num];` -- This is not valid C++. Arrays must have their size denoted by a compile-time value, not a runtime value. You are already using `std::vector`, so why not use it here? `std::vector l(num);`. You make the same error here: `Login lg[N];`. – PaulMcKenzie Jun 18 '22 at 16:31
  • Your classes do not have any fields, hence their objects do not store anything. Instead, you have a few global variables that are shared by all objects. Move them into the class. – Nico Schertler Jun 18 '22 at 16:40
  • do this will resolve my problem completely? and thank you for your suggestion for using vector of object in program.... @PaulMcKenzie – Siddharth Lodha Jun 18 '22 at 16:41
  • Okay, i will do it .... thank you @NicoSchertler – Siddharth Lodha Jun 18 '22 at 16:46

0 Answers0