-2

这是我的程序的完整代码和下面说明的问题。

这段代码来自我的“虚拟 ATM 机”程序,该程序处理客户存款、检查余额和从他们的账户取款。当我存钱时,它显示它已存入。但是“customer_actions()”是客户的主菜单,当我返回该屏幕并选择检查余额的选项时,它显示为零。

这意味着值没有从前一个函数更新。当用户输入存款金额时,我想让它更新:/

问题出在存款功能上,我猜。但是我发布了完整的程序供您使用。

非常感谢大家:)

头文件:

#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;
           }    
*/
};


#endif

到现在为止的完整程序:

#include <stdlib.h>
#include <conio.h>
#include <iostream>
#include <fstream>
#include "bank.h"

using namespace std;
using std::string;

int logincheck (int ,int );
double checkbal (double );
int mainmenu(int );
int customer_func();
int officer_func();
int enroll();
int customer_actions();



main() {

   double balance = 0, deposit = 0, withdraw = 0;
   int atmno;
   bankAccount b;


    system("cls");
    cout << endl;
    cout << "**************** Please select an ATM to continue *********************  \n"<<endl;;
    cout << "-----------------------------------------------------------------------  \n";
    cout << "|          ATM 1                  ATM 2                  ATM 3        |  \n";
    cout << "-----------------------------------------------------------------------  \n"<<endl;

    cout << "Enter ATM number: ";
    cin >> atmno;

    switch(atmno)
    {
        case 1: mainmenu(atmno); break;
        case 2: mainmenu(atmno); break;
        case 3: mainmenu(atmno); break;
    }
}

   int mainmenu(int atmno)
   {

   int mainselection;
   system("cls");
   // Menu
   cout << " -----------------------------------------------------------------------  \n";
   cout << "|                               ATM Booth: " << atmno << "                            | \n";
   cout << " ----------------- ----------------- ----------------- -----------------  \n";
   cout << "                     Please Select option to continue:              \n" << endl << endl;

   cout << "1) Customer Login    : Press 1" << endl;
   cout << "2) Officer Login     : Press 2" << endl;
   cout << "\nEnter option: ";
   cin >> mainselection;

   switch(mainselection)
   {
        case 1: customer_actions(); break;
        case 2: officer_func(); break;
   }

}

    int customer_func()
    {
        int usr;
        int pss;
        bankAccount b;


           system("cls");

           cout << " -----------------------------------------------------------------------  \n";
           cout << "|                        AUTOMATED TELLER MACHINE                       | \n";
           cout << " -----------------------------------------------------------------------  \n";
           cout << "|           Please enter username and password to continue.             | \n";
           cout << "|                                                                       | \n";
           cout << "|                                                                       | \n";
           cout << "|                                                                       | \n";
           cout << " -----------------------------------------------------------------------  \n\n";

           cout << "Username: ";
           cin >> usr;

           cout << "Password: ";

           char c = ' ';
           while(c != 13) //Password masking with *
             {
              c = getch();
              pss += c;
              cout << "*";
              }

           cin >> pss;

           //logincheck (usr, pss);


           getch();
        }

/*
int logincheck (int usr, int pss, bankAccount b)
{




}
*/



int officer_func()
{
    int officer_selection;
system("cls");
   // officer menu
   cout << " -----------------------------------------------------------------------  \n";
   cout << "|                              Officer Menu                             | \n";
   cout << " ----------------- ----------------- ----------------- -----------------  \n";
   cout << "                     Please Select option to continue:              \n" << endl << endl;

   cout << "1) Enroll new customer    : Press 1" << endl;
   cout << "2) Return home            : Press 2" << endl;
   cout << "\nEnter option: ";
   cin >> officer_selection;


   switch(officer_selection)
   {
        case 1: enroll(); break;
        case 2: main(); break;
   }



}

int enroll()
{
    bankAccount b;

    system("cls");

   cout << " -----------------------------------------------------------------------  \n";
   cout << "|                          Enroll new customer                          | \n";
   cout << " ----------------- ----------------- ----------------- -----------------  \n";
   cout << "                     Please Select option to continue:              \n" << endl << endl;



   ofstream myfile;

   b.balance = 0;
   cout << "Please enter customer name: " << endl;
   cin >> b.name;
   cout << "Please enter customer address: " << endl;
   cin >> b.address;
   cout << "Please enter customer Account Number: " << endl;
   cin >> b.accNo;
   cout << "Please enter username: (ONLY Numerals)" << endl;
   cin >> b.username;



  //generatepass(bankAccount& b);



   b.balance = 500000;



   int genpass = (b.accNo + 10);
   b.password = genpass;

   cout << "\nPassword Generated: " << genpass;

   myfile.open ("accounts.txt");
   myfile << b.name << endl << b.address << endl << b.accNo << endl << b.username << endl << genpass << endl << b.balance;
   myfile.close();    

   cout << "\n\nCustomer Account created! Press any key to save account file";
   getch();

   officer_func();

}



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: 
           for(int i=0;i<=20;i++)
           {
                   b.deposit(); 
           break;
           }    
      case 4: break;
      case 5: main(); break;   
    }

}


double bankAccount::checkbalance()
{
       system("cls");
       cout << " -----------------------------------------------------------------------  \n";
       cout << "|                              Customer Menu                            | \n";
       cout << " ----------------- ----------------- ----------------- -----------------  \n";

       cout << "\n\nYour balance is: " << balance;

       getch();
       customer_actions();
       return balance;
}


double bankAccount::withdraw()
{
       system("cls");
       cout << " -----------------------------------------------------------------------  \n";
       cout << "|                              Customer Menu                            | \n";
       cout << " ----------------- ----------------- ----------------- -----------------  \n";


       cout << "\n\nEnter Withdraw amount: ";
       cin >> withdrawamt;
       if (balance > withdrawamt)
          balance = (balance - withdrawamt);
       else {
            cout << "\nSorry you do not have enough money to make this transaction!";
            getch();
            customer_actions();
            }

       cout << "\nYou have withdrawn " << withdrawamt << " successfully!";
       cout <<"\nYour balance is: " << balance;

       getch();
       customer_actions();
       return balance;  
}


double bankAccount::deposit()
{


       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();
       return balance;

}

我已经“注释”掉的代码是我用于测试的代码或我仍在处理的代码,目前并不真正需要(Y)

4

1 回答 1

1

您需要将您bankAccount从一个功能传递到另一个功能。这可以通过传递 abankAccount&作为函数的参数来完成,以便传递相同的对象。您修改bankAccount当前使用的local银行帐户的函数在离开函数范围时被丢弃!

考虑到应用程序的简单性,一个丑陋的黑客可能是使用全局变量。

于 2013-05-23T13:50:58.927 回答