简体   繁体   English

如何使用struct C ++获得函数内部生成的最大值

[英]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. 没有名为AccountNumberBalanceDaysSinceDebited ,它们只是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.

相关问题 如何使用C ++中的函数更新结构的值? - how can i update value of a struct using a function in C++? 如何将结构从C#托管结构传递到C ++非托管DLL并得到结果结构? - How can I pass struct from C# managed to C++ unmanaged DLL and get struct in result? 我如何用可以在c ++中更改的函数制作结构? - How can I make a struct with a function that can be changed in c++? 如何从 c++ 中的 get function 返回多个结构值? - how to return multiple values of struct from get function in c++? 如何获取C ++中从函数返回的数组中输入的整数总数以及最低和最高分数? - How do i get the total number of integers entered into an array returned from a function in C++, as well as the lowest highest and average scores? 如何将结构字段从 Matlab 加载到 C++? - How can I load struct fields from Matlab to C++? 如何使用 matlab 数据 Z8A5DA52ED126447D359E70A8A55 中的可变数量字段在 C++ 中初始化 object - How can I initialize an object in C++ using a variable number of fields from a matlab data api struct 在 C++ 中使用 C 风格的 struct/typedef - Using C-style struct/typedef from within C++ 我如何使用 C++ 从寄存器中读取值 - how can i read value from register using C++ 如何从 C++ 中的函数返回结构? - How to return a struct from a function in C++?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM