这段代码来自我的“虚拟 ATM 机”程序,该程序处理客户存款、检查余额和从他们的账户取款。当我存入钱时,它显示它已被存入。但是......在我陈述我的问题之前,这里是代码:
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()”是客户的主菜单,当我返回该屏幕并选择检查余额的选项时,它显示为零。这意味着值没有从前一个函数更新。这是我的头文件,其中包含类文件:
#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
我在想,当程序从一个菜单切换到另一个菜单时,这些值会被重置。亲爱的,有什么建议吗?
提前致谢!
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;
}
}