hey guys Im here again :P
Here is my code for bank account class..

#include<iostream>
#include<iomanip>

using namespace std;

class account
{
    private:
        int account_balance;
    public:
        account(int acc) //constructor
        {
            if(acc>=0)
            {
             account_balance=acc;
            }
            else
            {
            account_balance=0;          
            cout<<"initial balance is invalid"<<endl;
            }
        }

        int credit(int ammount)  //credit member function
        {
        return account_balance+ammount;
        }

        int debit(int deb)  //debit member function
        {
            if(deb<=account_balance)
            {
            return account_balance-deb;
            }
            else
            {
            return account_balance;
            cout<<"Debit ammount exceeded account balance"<<endl;   
            }
        }

};

/* main body 
*/
int main()
{
 int acc,ammount,deb;
 cout<<"enter account balance"<<endl;
 cin>>acc;
 account obj(acc);
 cout<<"enter credit ammount"<<endl;
 cin>>ammount;
 cout<<"after adding credit ammount, your account balance is:"<<setw(5)<<obj.credit(ammount)<<endl;
 cout<<endl<<"enter debit ammount"<<endl;
 cin>>deb; 
 cout<<"after deducting debit ammount, your account balance is:"<<setw(5)<<obj.debit(deb)<<endl;

 return 0;
}

there is no any error in my code but logical error. Problem is when i enter credit ammount it does not adds it into account balance. And when i enter debit ammount it does not deducts it from account balance as well. Anyone please help.Im stuck here..

The account_balance is what you're trying to change, right?

So here are all the lines in your code where the account_balance is changed:

Line 15: account_balance=acc;
Line 19: account_balance=0;

So where in your function credit do you think you are changing account_balance?

yes, in line 26 :

return account_balance+ammount;

but it is not adding the ammount in account_balance.

There is some kind of logical error here which i am not figuring out.
I am passing the ammount in parameter to member func. credit ...and i know the method I used is obvious but why the answer is not right? :(

Line 26 does NOT change the value of account_balance. Let's take a look at it.

return account_balance+ammount;

So what's happening on the right? You're taking a number, account_balance, and you're taking another number, ammount, and you're adding them together to create a new number. Did you change the value of account_balance? No. You just used it in your addition. Did you change the value of ammount? No, you just used it in your addition.

Here is how to change the value of a variable:
x = 4; // this changes the value of x

So here is how to change the value of account_balance:
account_balance = something; // this changes the value of account_balance to be the same as something

:o Moschops You're amazing ..
I knew there is some logical mistake.I tried this:

    int credit(int ammount)  //credit member function
        {
        return account_balance=account_balance+ammount;
        }

and it actually changed the value of account_balance :D
thank you..

A far less confusing way to write that would be

int credit(int ammount)  //credit member function
{
  account_balance = account_balance+ammount;
  return account_balance;
}

although I think you should have a separate function entirely for fetching the account balance. What if someone just wants to know what their account balance is?

Oh yeah I'm still working on it..There are loads of changing to do.

What if I use switch in main() because may be user either tells credit ammount or debit ammount?

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.