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

时间:2017-11-06 17:26:08

标签: c++ class pointers object

Hello StackOverflow社区。 我似乎无法改变一个类的值。 我是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()
{
}

这是我的main.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 Deposit\n"
        << " 4 - Make a Withdraw\n"
        << " 5 - Check log\n"
        << " 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处保持不变。 有什么帮助吗?

1 个答案:

答案 0 :(得分:0)

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

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