for循环帮助计算一个值

时间:2016-02-07 18:51:49

标签: c++ loops for-loop

目前我有一个程序可以计算一系列帐户的利息。我需要程序做的是在控制台的末尾显示最感兴趣的帐户,我在使用for循环时遇到了一些问题,但我认为我差不多正确。

#include<iostream>


using namespace std;

int initialise(int number, double balance, int daysSinceDebited); //function    in the struct, used to initialise the array information

struct Account //name of the struct
{

int Number; //members of the struct and their data types
double Balance;
int DaysSinceDebited;

int initialise(int number, double balance, int daysSinceDebited) //declared function that initialises array data
{
    Number = number;
    Balance = balance;
    DaysSinceDebited = daysSinceDebited;

};


 };



int main() {

Account accountlist[8] = {
{ 1001, 4154.40, 20 },{ 7940, 270006.25, 35 },{ 4382, 123.50, 2 },{ 2651, 85326.92, 14 },{ 3020, 657.0, 5 },{ 7168, 657.0, 5 },{ 6245, 4.99, 1 },{ 9342, 107864.44, 45}
}; //array of accounts in the struct 



float interest = 0;

cout << "Account Number\t" << "Balance\t" << "Days\t" << "interest" << endl; //prints out the labels

for (int i = 0; i < 8; i++)
{
    if (accountlist[i].Balance > 10000 || accountlist[i].DaysSinceDebited > 30) //uses the array
        interest = (accountlist[i].Balance * 0.06);
    else
        interest = (accountlist[i].Balance * 0.03);

    cout << accountlist[i].Number << "\t\t" << accountlist[i].Balance << "\t" << accountlist[i].DaysSinceDebited << "\t" << interest << "\t" << endl;
}
    for (int i = 0; i < 8; i++) {
        if (accountlist[i].Number > interest)
            interest = accountlist[i].Number;

        cout << "The highest interest account is: " << interest << endl;



}


    system("Pause");
    return 0;



};

2 个答案:

答案 0 :(得分:0)

   for (int i = 0; i < 8; i++) {
        if (accountlist[i].Number > interest)
            interest = accountlist[i].Number;
   }
        cout << "The highest interest account is: " << interest << endl;

更改括号的位置。

答案 1 :(得分:0)

您所要做的就是将环的末端支架放在正确的位置。 这应该是在cout之前而不是之后。