[英]How can I get the highest value generated from within a function using struct C++
So I'm trying to display the account with the highest generated interest but for some reason it isn't working? 因此,我试图显示产生最高兴趣的帐户,但是由于某种原因,它无法正常工作? Any help is much appreciated. 任何帮助深表感谢。 The errors I'm getting are to do with the if statement at the bottom. 我遇到的错误与底部的if语句有关。
#include "stdafx.h"
#include <iostream>
using namespace std;
const int MAXACCOUNTS = 8;
struct Account
{
int AccountNumber;
double Balance;
int DaysSinceDebited;
double Interest;
};
int main()
{
double highestInterest = 0;
int highestInterestAccountIndex;
Account accounts[MAXACCOUNTS] = { {1001,4254,40,20},{7940,27006.25,35},{4382,123.50,2},{2651,85326.92,14},{3020,657.0,5},{7168,7423.34,360},{6245,4.99,1},{9342,107864.44,45} };
for (int index = 0; index < MAXACCOUNTS; index++)
{
if (accounts[index].Balance > 10000 || accounts[index].DaysSinceDebited > 30)
{
accounts[index].Interest = accounts[index].Balance * 0.06;
}
else
{
accounts[index].Interest = accounts[index].Balance * 0.03;
}
cout << "Account number: " << accounts[index].AccountNumber << " Balance: " << accounts[index].Balance << " Interest Paid: " << accounts[index].Interest << endl;
}
for (unsigned int index = 0; index < MAXACCOUNTS; index++)
{
accounts[index] = { AccountNumber[index], Balance[index], DaysSinceDebited[index] };
double increment = CalcInterest(accounts[index]);
if (increment >= highestInterest)
{
highestInterest = increment;
highestInterestAccountIndex = index;
}
}
std::cout << "The account that generated the greatest interest was account " << accounts[highestInterestAccountIndex].accountNumber << " With a total interest of " << highestInterest;
system("pause");
return 0;
}
I have to do it in this format as well, using the struct but I cant figure out where I'm going wrong. 我也必须使用该结构以这种格式进行操作,但我无法弄清楚哪里出了问题。
Get rid of these lines: 摆脱这些行:
accounts[index] = { AccountNumber[index], Balance[index], DaysSinceDebited[index] };
double increment = CalcInterest(accounts[index]);
You already filled in accounts[index]
when you initialized it at the beginning of main()
. 在main()
的开头对其进行初始化时,您已经填写了accounts[index]
。 There are no arrays named AccountNumber
, Balance
, or DaysSinceDebited
, those are only members of the Account
struct. 没有名为AccountNumber
, Balance
或DaysSinceDebited
,它们只是Account
结构的成员。
And there's no function CalcInterest()
, the interest is already in the structures. 并且没有函数CalcInterest()
,兴趣已经存在于结构中。 So you can change that second line to simply: 因此,您可以将第二行更改为:
double increment = accounts[index].Interest;
You also have a typo: accounts[highestInterestAccountIndex].accountNumber
in the last cout
line should be accounts[highestInterestAccountIndex].AccountNumber
. 你也有一个错字: accounts[highestInterestAccountIndex].accountNumber
在最后cout
线应accounts[highestInterestAccountIndex].AccountNumber
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.