C++.访问对象和设置值
C++ . Accessing Objects and Setting Values
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;
}
相关文章:
- 如何在自删除后将对象设置为nullptr
- QT:即使在static_cast后也无法在主窗口中设置对象的位置
- 将对象传递给 Java C++与使用 JNI 逐个设置对象参数
- V8 无法使用名称设置对象模板"console"
- 从函数返回 libconfig 类设置对象引用
- 当设置对象等于另一个函数的返回值时,为什么要调用移动构造函数/分配
- 在功能中设置对象变量
- 如何根据道具名称动态设置对象的属性?
- 如何传递unique_ptr来设置对象的成员变量?
- C++ 仅设置对象指向 nullptr 的指针
- 如何在涡轮C 图形中设置对象的自定义边框宽度
- 无法在 cocos2dx 中为 CCDictionary 设置对象
- 位掩码:通过集合方法设置对象的不同状态
- C++:如何从成员中设置对象
- 使用运算符设置对象>>
- 如何设置对象数据成员与这个c++与这个
- Q设置:移动/更改现有Q设置对象的范围/位置
- 在第一个可用索引处设置对象到C数组
- 如何设置对象转换,属性
- 在 C# 等C++中设置对象 = null 的替代方案是什么