with a lil bit of change in code ,it's functioning, but I want it to function with data members being private :-/

#include<iostream.h>
#include<conio.h>
class account{
public:
int account_number;
float balance;
int passw;
public:
void display()
{
cout<<"Account number is:"<<account_number<<endl<<
"Account balance is:"<<balance<<endl;
}
void user(int acc, float amou, int passy)
{
account_number=acc;
balance=amou;
passw=passy;
}
void validate(int acce, int passu)
{
account_number=acce;
passw=passu;
}
};
void main()
{
int acno;
float amount;
int const n=1;
account uxi[n];
int pass;
for(int i=0;i<n;i++)
{
cout<<"Enter account number";
cin>>acno;
cout<<"set the password for your account";
cin>>pass;
cout<<"Enter the amount you want to deposit";
cin>>amount;
uxi[i].user(acno,amount,pass);
}

for(i=0;i<n;i++)
{
cout<<"Enter the account number";
cin>>acno;
if(acno==uxi[i].account_number)
{
cout<<"Enter the password for the account";
cin>>pass;
}
if(pass!=uxi[i].passw);
{
cout<<"the password you entered is incorrect";
if(pass==uxi[i].passw)
uxi[i].display();

}
};
}

Recommended Answers

All 4 Replies

Can't read it. After 140+ posts you should understand formatting by now.

Once you change the scope of account_number, balance and passw to private scope,
you won't be able to access them directly from main().
So you will have to add public accessor functions for each of the above data members, and call them in main().

example:

class account{
private:
      int account_number; //account_number's scope is private

public:
      int GetAccountNumber( ){ return account_number; } // public accessor function
 };

 void main()
 {
 ...
        //if(acno==uxi[i].account_number) // Can't access account_number directly
        if(acno==uxi[i].GetAccountNumber())//Using the public accessor function instead
}

use what WolfPack told you:

//class ....{
    int acc() { 
        return (account_number);
    }

    //etc.
}

Also

uxi[i].user(acno,amount,pass);

will be useles. I suggest you to use the vector class ftom std to store your objects.

And, BTW, took me 5 minutes to see where the class ends, and where the main function starts, and also, the ';' at the end of the code are useless. Please, use proper indentation. Your class should look like this:

class account{
    int account_number;
    float balance;
    int passw;
public:
    void display(){
        cout<<"Account number is:"<<account_number<<endl
            <<"Account balance is:"<<balance<<endl;
    }

    int acc(){
        return (account_number);
    }

    float bal(){
        return (balance);
    }

    int pas(){
        return (passw);
    }

    void user(int acc, float amou, int passy){
        account_number=acc;
        balance=amou;
        passw=passy;
    }

    void validate(int acce, int passu){
        account_number=acce;
        passw=passu;
    }
};

And also with the password, I suggest you either make it in a string and than compare the characters, or add some kind of validation so that the user would not be able to insert characters. Now in your menu if I type 'a' at the password field it will just terminate the program (it will crash it).

THANKS ALOT GUYS, IT WORKED LIKE A CHARM

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.