如何使用结构C++获取从函数中生成的最大值

How can I get the highest value generated from within a function using struct C++

本文关键字:最大值 函数 何使用 结构 C++ 获取      更新时间:2023-10-16

所以我试图显示产生最高兴趣的帐户,但由于某种原因它不起作用?任何帮助都非常感谢。 我得到的错误与底部的 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;
}

我也必须使用结构以这种格式执行此操作,但我无法弄清楚我哪里出错了。

去掉这些行:

accounts[index] = { AccountNumber[index], Balance[index], DaysSinceDebited[index] };
double increment = CalcInterest(accounts[index]);

您在main()开头初始化它时已经填写了accounts[index]。没有名为AccountNumberBalanceDaysSinceDebited的数组,它们只是Account结构的成员。

而且没有功能CalcInterest(),兴趣已经在结构上了。 因此,您可以将第二行更改为:

double increment = accounts[index].Interest;

您还有一个错别字:最后cout行中的accounts[highestInterestAccountIndex].accountNumberaccounts[highestInterestAccountIndex].AccountNumber