C++.访问对象和设置值

C++ . Accessing Objects and Setting Values

本文关键字:设置 对象 访问 C++      更新时间:2023-10-16

Hello StackOverflow community.我似乎无法更改类的值。我是C++新手,我一直在寻找解决方案,但我似乎无法弄清楚。

This is my Account.cpp
    #include "Account.h"
#include <iostream>
using namespace std;

Account::Account(string accName, int accBalance)
{
    name = accName;
    balance = accBalance;
    std::vector<std::string> Report();
}

bool Account::Deposit(Account a,int amt) {
    if (amt >= 0) {
        a.balance += amt;
        return 1;
    }
    else
        return 0;
}
bool Account::Withdraw(Account a, int amt) {
    if (amt >= 0 && (a.balance - amt > 0)) {
        a.balance -= amt;
        return 1;
    }
    else
        return 0;
}
int equal(string a, string b) {
    if (a == b) return 1;
    else return 0;
}
Account::~Account()
{
}

这是我的主要.cpp

int main() {
int aux_bal;
string aux_name;
int choice;
std::string InputName;
std::list< Account > arr;
std::list<Account>::iterator result;
do
{

    cout << endl
        << " 1 - Create New Account.n"
        << " 2 - View Balance.n"
        << " 3 - Make a Depositn"
        << " 4 - Make a Withdrawn"
        << " 5 - Check logn"
        << " 6 - Exit.n"
        << " Enter your choice and press return: ";
    cin >> choice;
    switch (choice)
    {
    case 1:
        cout << "Input holder's account name: n";
        cin >> InputName;
        arr.push_back(Account(InputName, 0));

        break;
    case 2:
        cout << "Enter Account Name:n";
        cin >> InputName;
        for (result = arr.begin(); result != arr.end(); result++) {
            int aux_bal = result->balance;
            std::string aux_name = result->name;
            if (aux_name == InputName) {
                cout <<"Account Balance:"<< aux_bal << endl;
                cout << "Account Holder:" << aux_name << endl;
            }
        }
            break;
    case 3:
        cout << "Enter Account Name:n";
        cin >> InputName;
        for (result = arr.begin(); result != arr.end(); result++) {
            int aux_bal = result->balance;
            std::string aux_name = result->name;
            if (aux_name == InputName) {
                cout << "Enter ammount to Deposit:n";
                cin >> aux_bal;
                (*result).Deposit(*result,aux_bal);
            }
        }
        break;
    case 4:
        //code to help the user like give him
        //extra information about the mode and the controller 
        break;
    case 5:
        break;
    case 6:cout << "End of Program.n";
        break;
    default:
        cout << "Not a Valid Choice. n"
            << "Choose again.n";
        break;
        }
    } while (choice != 6);
    return 0;
}

由于某种原因,当我在存款后输出帐户信息时,余额值保持不变,为 0。请帮忙吗?

存款函数应该作用于它所属的帐户对象,您不需要将其作为参数传递。此外,整数不是布尔值。最后,为什么存款金额是整数?如果我想存入 $20.25 怎么办?

bool Account::Deposit(int depositAmt)
{
    if (depositAmt >= 0) {
        this->balance += depositAmt; // "this->" is optional
        return true;
    }
    else
        return false;
}