循环/结构中的 C++ 函数

标签 c++ function loops struct

这是我未更改的工作代码:

#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 等请帮助!

最佳答案

我相信“有很多错误”是指编译错误。快速查看您的代码可以确认这一点。

您犯的第一个错误是此功能仅适用于单个帐户。因此,您不能在该函数内遍历您的帐户数组,也不能访问 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.
}

请注意,我在这里只提供了语言结构,因为这显然是某种作业。我已经指出了您需要做一些工作的部分。

关于循环/结构中的 C++ 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32321667/

相关文章:

c++ - 使用虚拟纯函数访问字段的段错误

javascript - 使用javascript跳 anchor

python - 如何将 python 脚本分成几部分并循环导入这些部分?

c - 在 C 中的 vector 函数序列中避免(初学者)分配错误

c++ - friend 关键字(类/函数)如何打破 C++ 中的封装?

python - 使用循环在Python中压缩多个文件

linux - 如何使用循环 ssh 多个服务器,但如果一个用户失败,则与两个用户尝试另一个

c++ - 使用带有 set_intersection 的 map

c++ - 我们在条件三元运算符中使用逗号时发现了什么?

c++ - CMake:使用 target_sources() 添加当前目录和子目录中的所有文件