如何使用结构C++获取从函数中生成的最大值
How can I get the highest value generated from within a function using struct C++
所以我试图显示产生最高兴趣的帐户,但由于某种原因它不起作用?任何帮助都非常感谢。 我得到的错误与底部的 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]
。没有名为AccountNumber
、Balance
或DaysSinceDebited
的数组,它们只是Account
结构的成员。
而且没有功能CalcInterest()
,兴趣已经在结构上了。 因此,您可以将第二行更改为:
double increment = accounts[index].Interest;
您还有一个错别字:最后cout
行中的accounts[highestInterestAccountIndex].accountNumber
应accounts[highestInterestAccountIndex].AccountNumber
。
相关文章:
- 如何创建一个函数来计算并返回平均值、最大值和最小值
- C++ 函数,用于查找传入的 N 个数字的平均值、总和、最小值和最大值
- 递归函数,用于查找 2 个整数之间的最大值
- 如何将最小值和最大值放入函数中
- C++数组的最大值、最小值、平均值函数
- 数组函数似乎没有找到最大值
- 如何使用结构C++获取从函数中生成的最大值
- C++中的通用最小值和最大 lambda 函数
- 为什么从矩阵中获取最大值的函数建议 ->无法将 'int (*)[C]' 转换为 'int (*)[0]
- SSE2 内部函数 - 找到两个无符号短向量的最大值
- 如何使用函数参数中的int指针来获取2d数组中最大值的索引并返回指针
- 查找使用最少探测器的函数的最大值
- 函数,它接受三个参数:指向双数组的指针、指向最小值的指针和指向最大值元素的指针
- 函数返回最大值的索引,跳过以前返回的值
- 试图通过函数查找数组的最大值和最小值,其中数组值由用户输入
- 一个模板函数,用于获取向量中的最大值及其相应的索引
- 模板函数如何获得整数或字符的最大值
- C++为向量制作最小值和最大值函数
- 函数,返回可变数量标量的最大值
- Std::数组查找最大值函数