在完成与BankAccounts的练习时,我的编程书遇到了一些麻烦。问题是:
创建一个main()函数,声明一个包含10个BankAccount对象的数组。后 输入第一个BankAccount对象,询问用户是否要继续。提示用户输入BankAccount值,直到用户想要退出或输入10个BankAccount对象为止,以先到者为准。输入的每个BankAccount,显示BankAccount详细信息。然后提示用户输入值介于1和40之间的术语。继续提示用户,直到输入有效值。 然后将BankAccount对象数组,数组中有效对象的数量以及所需术语传递给一个函数,该函数计算并显示每个BankAccounts 的值,在给定的年数之后标准利率。
我必须使用3个公共函数来完成此任务。当我尝试使用computeInterest()时,我才开始遇到问题。如果我尝试传递该术语并计入该函数并将其设置为2 for for循环,我将只接收单个对象的数字和余额。我想我要问的是:如何让程序调用computeInterest并为每个BankAccount对象运行?
目前我的代码已设置好以便编译并运行得非常好,但是,问题要求在我发送该术语时将术语和计数发送到该函数。
我不确定我是否将对象数组发送到函数中?这可能是造成麻烦的原因。这已经让我的大脑磨了好几天了 :( 。非常感谢任何帮助/指针!
#include<iostream>
#include<string>
using namespace std;
// Declaration Section
class BankAccount
{
private:
int accNum;
double accBal;
const static double intRate;
public:
void enterAccountData(int, double);
void computeInterest(int);
void displayAccount();
};
// Implementation Section
const double BankAccount::intRate = 0.03;
void BankAccount::enterAccountData(int num, double bal)
{
const int MIN_ACC = 1000, MAX_ACC = 9999, MIN_BAL = 0,
MAX_BAL = 100000;
cout << "Enter account number: ";
cin >> num;
while (num < MIN_ACC || num > MAX_ACC)
{
cout << "ERROR: Account number must be between " << MIN_ACC <<
" - " << MAX_ACC << ".\n" << "Enter account number: ";
cin >> num;
}
cout << "Enter account balance: $";
cin >> bal;
while (bal < MIN_BAL || bal > MAX_BAL)
{
cout << "ERROR: Account balance must be positive and less than $" <<
MAX_BAL << ".\n" << "Enter account balance: $";
cin >> bal;
}
accNum = num;
accBal = bal;
}
void BankAccount::computeInterest(int YR)
{
int x;
double interest;
for (x = 0; x < YR; ++x)
{
interest = accBal * intRate;
accBal = accBal + interest;
cout << "Account# " << accNum <<
", Balance: $" << accBal << ", Interest: " <<
intRate << endl;
}
cout << endl;
}
void BankAccount::displayAccount()
{
cout<<"Account: " << accNum <<", Balance: $" << accBal <<
", Interest: " << intRate << endl << endl;
}
int main()
{
const int END = -999, MAX_ACCOUNTS = 10, CONTINUE = 1, MIN_TERM = 1,
MAX_TERM = 40;
int i, x, count = 0, num = 0, term = 0;
double bal = 0;
BankAccount accounts[MAX_ACCOUNTS];
do
{
count++;
accounts[count - 1].enterAccountData(num, bal);
accounts[count - 1].displayAccount();
cout << "Enter " << CONTINUE << " to proceed, or " << END << " to stop: ";
cin >> i;
if (i == END || count == MAX_ACCOUNTS)
{
cout << "Enter the term of the accounts (" << MIN_TERM <<
"-" << MAX_TERM << "): ";
cin >> term;
while (term < MIN_TERM || term > MAX_TERM)
{
cout << "ERROR: Term must be between " << MIN_TERM <<
" - " << MAX_TERM << " inclusive.\n" <<
"Enter term of the accounts (" << MIN_TERM <<
"-" << MAX_TERM << "): ";
cin >> term;
}
for (x = 0; x < count; ++x)
accounts[x].computeInterest(term);
}
} while (i != END && count != MAX_ACCOUNTS);
}
答案 0 :(得分:0)
你已经在做了
for (x = 0; x < count; ++x)
accounts[x].computeInterest(term);
如果确实有必要,您可以将该特定代码段移动到一个单独的函数中,该函数接受3个参数:数组accounts
,count
和term
。
您设置BankAccount类的方式是每个对象计算自己的兴趣。 由于你有几个这样的对象,你可以要求每个人计算自己的兴趣。
希望这有帮助!如果还有其他问题,请告诉我们。)