PewK
PewK

Reputation: 397

C++ Values not been stored as needed

This code is from my "virtual ATM machine" program which deals with customers depositing, checking balance and withdrawing money from their account. When I deposit the money, it displays that it gets deposited.. But... here goes the code before I state my problem:

double bankAccount::deposit()
{
      bankAccount b;

       double amt;

       system("cls");
       cout << " -----------------------------------------------------------------------  \n";
       cout << "|                              Customer Menu                            | \n";
       cout << " ----------------- ----------------- ----------------- -----------------  \n";

       cout << "\n\nYOUR CURRENT BALANCE: " << balance << endl;
       cout << "\nEnter amount to deposit: ";
       cin >> amt;

       balance = (balance + amt);

       cout << "\nAmount depositted successfully!" << endl;
       cout <<"\nYOUR CURRENT BALANCE: " << balance;

       getch();
       customer_actions();
       return balance;

}

"customer_actions()" being the main menu for the customers, when I go back on that screen and select the option to check balance, it displays as ZERO. Which means the values didn't get updated from the previous function. Here's my header file which consists of the class file:

#ifndef bank
#define bank


using namespace std;


class bankAccount
{
    public:
        int accNo;
        int password;

        double balance;
        double withdrawamt;
        double depositamt;

        char name[20];
        char address[40];
        char username[10];

    public:

        double checkbalance();
        double deposit();
        double withdraw();


    public:
           bankAccount()
           {
              balance = 0; // Is this the reason?
           }    

};


#endif

I'm thinking, when the program switches from one menu to the other, the values get reset-ed. Any suggestions, dear folks?

Thanks in advance!

CUSTOMER_ACTIONS:

int customer_actions()
    {
          bankAccount b;
          int cust_selection;
          system("cls");

   cout << " -----------------------------------------------------------------------  \n";
   cout << "|                              Customer Menu                            | \n";
   cout << " ----------------- ----------------- ----------------- -----------------  \n";
   cout << "                     Please Select option to continue:              \n" << endl << endl;

   cout << "1) Check balance    : Press 1" << endl;
   cout << "2) Withdraw Cash    : Press 2" << endl;
   cout << "3) Deposit Cash     : Press 3" << endl;
   cout << "4) Transfer Cash    : Press 4" << endl;
   cout << "5) Return home      : Press 5" << endl;
   cout << "\nEnter option: ";
   cin >> cust_selection;               


   switch(cust_selection)
   {
      case 1: b.checkbalance();   break;
      case 2: b.withdraw(); break;
      case 3: b.deposit(); break;
      case 4: break;
      case 5: main(); break;   
    }

}

Upvotes: 0

Views: 263

Answers (2)

ose
ose

Reputation: 4075

Your problem (from what I can see) is that you are trying to create an infinite loop where the user can keep pressing making changes on the menu until they exit. However you are going about this by calling customer_actions() from within the deposit function.

Try creating an infinite loop in an outer method, then returning from deposit without calling customer_actions().

Following OP edit

Try this:

int main(...)
{
     int result = 0;
     while(result == 0)
     {
         result = customer_actions();
     }
}

Now change the switch statement in customer_actions to be like this:

switch(cust_selection)
{
      case 1: b.checkbalance();   break;
      case 2: b.withdraw(); break;
      case 3: b.deposit(); break;
      case 4: break;
      case 5: return 1; // This is the change
}
return 0;

Upvotes: 1

Solkar
Solkar

Reputation: 1226

The bank account b you declare in customer_action is just valid in function scope.

In addition:

customer_action manages given accounts by their interface, accounts should not "manage" customer_action (in your case, don't call it from deposit)

You could easily get a stack overflow the way you coded that.


Generally speaking you should try to avoid mixing up model (your accounts) view (the output) and controller (user input) - related code.

Create clean interfaces and call in in a structured manner.


In addition:

I first read your bold put question and afterwards attended the code.

The first thing I did then was was trying to find if there is local redefinition of double balance. There is none, but I should not even have had to do so, because there are means to avoid side-effects on instance member variables like balance.

Foremost - make them private, not public.

Then:

  1. use a prefix like m_ so m_balance that would be or even m_dblBalance to indicate the type
  2. or prefix all private variables with a _, so _balance that would be
  3. and/or emphasize each usage of instance member vars by prefixing them with a redundant this->

Personally speaking I dislike 1. but use 2. for instance variables.


There are many more design and implementation issues, eg. I would discourage char[] for strings and recommend using std::string, but maybe you start by just ending the marriage of deposit() and customer_actions() you blessed.

Upvotes: 0

Related Questions