C++ 值未按需要存储

标签 c++ class compilation

此代码来 self 的“虚拟 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

我在想,当程序从一个菜单切换到另一个菜单时,值会被重置。有什么建议吗,亲爱的 friend 们?

提前致谢!

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;   
    }

}

最佳答案

您的问题(据我所知)是您试图创建一个无限循环,用户可以在该循环中不断按下在菜单上进行更改,直到他们退出。但是,您将通过在存款函数中调用 customer_actions() 来解决这个问题。

尝试在外部方法中创建一个无限循环,然后从 deposit 返回而不调用 customer_actions()

在 OP 编辑​​之后

试试这个:

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

现在将 customer_actions 中的 switch 语句更改为如下所示:

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;

关于C++ 值未按需要存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16712214/

相关文章:

jquery - 类不会使用 jQuery 进行交换

java - 我的成员变量在 Eclipse 中用红色下划线表示

macos - 如何从终端调用文本编辑器?

c++ - 使用结构调用需要独立参数的 C++ 函数是否安全?

c++ - Eigen 3.3 对 mingw-w64 (GCC 7.1.0) 有很多警告

c++ - 成员函数指针和继承

c++ - 为什么只有在符合 "this"条件时,数据成员的模板函数才是从属名称?

Java - 新对象定义读取旧对象

assembly - 编译器通常使用寄存器来实现 "intended"的目的吗?

c++ - 为什么我会出现堆栈溢出?建议将不胜感激