这是我未改变的工作代码:
#include <iostream>
using namespace std;
const int MAXACCOUNTS = 8;
int interest(int Balance, int MAXACCOUNTS);
struct Account
{
int Number;
double Balance;
int DaysSinceDebited;
};
int main()
{
int Accountnumber;
double Balance;
int DaysSinceDebited;
double Total[MAXACCOUNTS] = {};
Account accounts[MAXACCOUNTS];
accounts[0].Number = 1001;
accounts[0].Balance = 4254.40;
accounts[0].DaysSinceDebited = 20;
accounts[1].Number = 7940;
accounts[1].Balance = 270006.25;
accounts[1].DaysSinceDebited = 35;
accounts[2].Number = 4382;
accounts[2].Balance = 123.50;
accounts[2].DaysSinceDebited = 2;
accounts[3].Number = 2651;
accounts[3].Balance = 85326.92;
accounts[3].DaysSinceDebited = 14;
accounts[4].Number = 3020;
accounts[4].Balance = 657.0;
accounts[4].DaysSinceDebited = 5;
accounts[5].Number = 7168;
accounts[5].Balance = 7423.34;
accounts[5].DaysSinceDebited = 360;
accounts[6].Number = 6285;
accounts[6].Balance = 4.99;
accounts[6].DaysSinceDebited = 1;
accounts[7].Number = 9342;
accounts[7].Balance = 107964.44;
accounts[7].DaysSinceDebited = 45;
for (int i = 0; i < MAXACCOUNTS; i++)
{
if ((accounts[i].Balance > 10000) || (accounts[i].DaysSinceDebited>30))
Total[i] = accounts[i].Balance * 1.06; //6% interest added
else Total[i] = accounts[i].Balance * 1.03; //3% interest added
cout << accounts[i].Number << " has a balance of " << accounts[i].Balance << ". The amount with interest is: " << Total[i] << endl;
system("pause");
}
}
这是我需要做的:你必须为你的程序添加一个名为CalcInterest的函数。此函数将其作为唯一参数作为帐户,并返回如第1部分所示计算的兴趣。您的主程序现在应该使用此函数,以生成第1部分中的显示。
这是我试过的:
#include <iostream>
using namespace std;
const int MAXACCOUNTS = 8;
int CalcInterest(Account);
struct Account { //declare struct outside of main
int Number;
double Balance;
int DaysSinceDebited;
};
int main()
{
int AccountNumber[MAXACCOUNTS] = { 1001, 7940, 4382, 2651, 3020, 7168, 6245, 9342 };
double Balance[MAXACCOUNTS] = { 4254.40, 27006.25, 123.50, 85326.92, 657.0, 7423.34, 4.99, 107864.44 };
int DaysSinceDebited[MAXACCOUNTS] = { 20, 35, 2, 14, 5, 360, 1, 45 };
double Total[MAXACCOUNTS] = {};
//add your code here
Account accounts[MAXACCOUNTS];
accounts[0].Number = 1001;
accounts[0].Balance = 4254.40;
accounts[0].DaysSinceDebited = 20;
accounts[1].Number = 7940;
accounts[1].Balance = 270006.25;
accounts[1].DaysSinceDebited = 35;
accounts[2].Number = 4382;
accounts[2].Balance = 123.50;
accounts[2].DaysSinceDebited = 2;
accounts[3].Number = 2651;
accounts[3].Balance = 85326.92;
accounts[3].DaysSinceDebited = 14;
accounts[4].Number = 3020;
accounts[4].Balance = 657.0;
accounts[4].DaysSinceDebited = 5;
accounts[5].Number = 7168;
accounts[5].Balance = 7423.34;
accounts[5].DaysSinceDebited = 360;
accounts[6].Number = 6285;
accounts[6].Balance = 4.99;
accounts[6].DaysSinceDebited = 1;
accounts[7].Number = 9342;
accounts[7].Balance = 107964.44;
accounts[7].DaysSinceDebited = 45;
CalcInterest(Account);
}
int CalcInterest(Account) {
for (int i = 0; i < MAXACCOUNTS; i++)
{
if ((accounts[i].Balance > 10000) || (accounts[i].DaysSinceDebited > 30))
Total[i] = accounts[i].Balance * 1.06;
else Total[i] = accounts[i].Balance * 1.03;
cout << accounts[i].Number << "has a balance of " << accounts[i].Balance << ". The amount with interest is : " << Total[i] << endl;
}
system("pause");
return 0;
}
这有很多错误,主要是事情变得不确定,例如.DaysSinceDebited等请帮助!
答案 0 :(得分:1)
我相信&#34;有很多错误&#34;,你在谈论编译错误。快速查看代码即可确认这一点。
您犯的第一个错误是此功能仅适用于单个帐户。因此,您无法遍历该函数内的帐户数组,也无法访问Total
。您对传递参数的语法以及应返回的数据类型也有点困惑。我可以帮助你。
将您的功能定义更改为:
double CalcInterest( const Account & account )
{
// Do your interest calculation on 'account' here, then return it from the function.
double interest = 0.0; //<-- For you to do.
return interest;
}
然后,您可以在main
...
for (int i = 0; i < MAXACCOUNTS; i++)
{
// Calculate the interest on the account, then do something with it.
double interest = CalcInterest( accounts[i] );
Total[i] = 0.0; //<-- For you to do.
}
请注意,我在这里只提供了语言结构,因为这显然是某种类型的赋值。我已经指出了你需要做一些工作的部分。