You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++银行系统编程:如何获取对象内wallet值在循环中更新显示

问题原因

你的wallet显示异常是三个逻辑错误共同导致的:

  • 存在两份完全独立的wallet变量:一份是main()函数内声明的局部变量,另一份是bank类的私有成员。类中存取款逻辑修改的是类自身的私有成员,和main中打印的局部变量没有任何关联,后者从未被赋值,显示0只是未初始化局部变量的随机结果。
  • 两个bank实例b1、b2各自持有独立的wallet成员,不符合业务逻辑:钱包是用户全局持有的资金池,不从属于单个银行账户,不管从哪个账户取钱、往哪个账户存钱,变动的都应该是同一个钱包余额。
  • 类内的wallet成员从未初始化,本身值是随机的,余额判断逻辑从根源上就不准确。
修复方法

核心调整逻辑

  1. 删除bank类内的私有wallet成员,钱包不属于单个银行账户的属性
  2. 在main中初始化唯一的钱包余额,将钱包以引用形式传入存取款函数,所有账户操作直接修改这一个全局唯一的钱包变量
  3. 修正原代码中main函数缺失返回值类型的不规范写法,标准C++要求main返回int类型

修正后可运行代码

#include <iostream>
using namespace std;

class bank
{  
private :
    int accnum;
    float currentbal;
    float amount;
        
public :
    bank(float bal) : currentbal(bal)
    {}

    // 接收钱包引用,直接操作唯一的钱包余额
    void deposit(float& wallet)
    {   
        cout << "\nHow much would to deposit:";
        cin >> amount;
        if (wallet < amount){
            cout << "\nInsufficient funds\n";
        } else {
            currentbal = currentbal + amount;
            cout << "new balance is $ " << currentbal << "\n";
            wallet = wallet - amount; 
        }
    }

    void withdraw(float& wallet)
    {   
        cout << "\ncurrent balance $ " << currentbal;
        cout << "\nHow much would to withdraw: ";
        cin >> amount;
        if (amount > currentbal){
            cout << "\nInsufficient funds\n";
        } else {
            currentbal = currentbal - amount;
            cout << "new balance is $ " << currentbal << "\n";
            wallet = wallet + amount; 
        }
    }

    void menu()
    {   
        cout << "\n1 to withdraw";
        cout << "\n2 to deposit";  
    }
};

int main()
{   
    int input;
    int input2;
    int input3;
    // 初始化钱包初始余额,可根据需求自行修改数值
    float wallet = 5000.0f;
    
    bank b1(1000);
    bank b2(2000);
    
    do {
        cout << "\nCurrent wallet: $ " << wallet << "\n";
        cout << "\nSelect bank account:\n";
        cout << "Select 1 for alpha";
        cout << "\nSelect 2 for beta";
        cout << "\nSelect 3 to exit\n";
        cout << "Input: ";
        cin >> input;
        switch (input) {
            case 1 :
            {   
                b1.menu();
                cout << "\nInput: ";
                cin >> input2;
                switch (input2) {
                    case 1 :
                        b1.withdraw(wallet);
                        break;
                    case 2 :
                        b1.deposit(wallet);
                        break;
                    default :
                        cout << "\nivalid input.\n";
                }
                break;
            }  
            case 2 :
            {   
                b2.menu();
                cout << "\nInput: ";
                cin >> input3;
                switch (input3) {
                    case 1 :
                        b2.withdraw(wallet);
                        break;
                    case 2 :
                        b2.deposit(wallet);
                        break;
                    default :
                        cout << "\nivalid input.\n";
                }
                break;
            }
            case 3 :
                cout << "\nThank you";
                break;
            default :
                cout << "\nPlease enter valid input.\n";
        }
    
    } while (input != 3);
    return 0;
}

补充说明

引用传参会直接把原变量传入函数,函数内对wallet的修改会直接作用到main中的原始变量,循环开头打印的钱包余额会随存取款操作实时更新,不存在不同步问题。如果不想修改函数参数,也可以用类静态成员实现所有账户共享同一个钱包余额,但对这个简化场景来说,引用传参的实现最直观,逻辑也最清晰。


内容的提问来源于stack exchange,提问作者ANTOINE

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.31 08:24:22