Using a function to return a value

I have this code at the moment that works and calculates the interest of the account depending on the conditions set. However, I now need to encode a function with a name CalcInterest()that takes an account as the only parameter and returns the calculated percentage.

#include <iostream>

using namespace std;

int main()
{

    int AccountNumber[8] = { 1001, 7940, 4382, 2651, 3020, 7168, 6245, 9342 };

    float Balance[8] = { 4254.40, 27006.25, 123.50, 85326.92, 657.0, 7423.34, 4.99, 107864.44 };

    int DaysSinceDebited[8] = { 20, 35, 2, 14, 5, 360, 1, 45 };
    int interest = 0;

    //add your code here

    cout << "Account Number\t" << "Balance\t\t" << "Days\t" << "Interest\t" << endl;


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

        cout << AccountNumber[i] << "\t\t" << Balance[i] << "\t\t" << DaysSinceDebited[i] << "\t" << interest << "\t" << endl;
    }

    system("pause");
    return 0;
}

Here is what I tried, the function does not work, but there are no errors

   #include <iostream>

 using namespace std;


 float CalcInterest(int AccountNum);



int main()

{



cout << "Account Number\t" << "Balance\t\t" << "Days\t" << "Interest\t" << endl;

float CalcInterest(int AccountNum);

system("pause");
return 0;
};



float CalcInterest(int AccountNum) {

int interest = 0;
float Balance[8] = { 4254.40, 27006.25, 123.50, 85326.92, 657.0, 7423.34, 4.99, 107864.44 };
int DaysSinceDebited[8] = { 20, 35, 2, 14, 5, 360, 1, 45 };
int AccountNumber[8] = { 1001, 7940, 4382, 2651, 3020, 7168, 6245, 9342 };


for (int i = 0; i < 8; i++)
{
if (Balance[i] > 10000 || DaysSinceDebited[i] > 30)
return interest = (Balance[i] * 0.06);
else
return interest = (Balance[i] * 0.03);
cout << AccountNumber[i] << "\t\t" << Balance[i] << "\t\t" << DaysSinceDebited[i] << "\t" << interest << "\t" << endl;
}





}
0
source share
3 answers

Firstly, a serious syntax error caused by copying and pasting code:

int main()
{
    /* ... */
    float CalcInterest(int AccountNumber);
    /* ... */
};

(1) CalcInterest(), , parens :

CalcInterest(5);

, , , , ( , . , , , : CalcInterest float int, AccountNumber. ? ++ .

:

float CalcInterest(int AccountNumber) {
    /* ... */
    int AccountNumber[8] = { 1001, 7940, 4382, 2651, 3020, 7168, 6245, 9342 };
    if (AccountNumber == 1001 /* ... */) { /* ... */ }

, (re) AccountNumber int 8, int AccountNumber , , 1001 int AccountNumber[8]. :

main.cpp: In function 'float CalcInterest(int)':
main.cpp:21:24: error: declaration of 'int AccountNumber [8]' shadows a parameter
     int AccountNumber[8] = { 1001, 7940, 4382, 2651, 3020, 7168, 6245, 9342 };
                        ^
main.cpp:33:30: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
         if (AccountNumber == 1001 || AccountNumber == 4382 || AccountNumber == 3020 || AccountNumber == 6245)
                              ^

, , for. , return , ! , ?

, , for ? :

#include <iostream>

using namespace std;

void CalcInterest(int i);

int main()
{
    cout << "Account Number\t" << "Balance\t\t" << "Days\t" << "Interest\t" <<    endl;
    for (int account = 0; account < 8; account++) {
        CalcInterest(account);
    }
    system("pause");
    return 0;
};

void CalcInterest(int i) {
    static const float Balance[8] = { 4254.40, 27006.25, 123.50, 85326.92, 657.0, 7423.34, 4.99, 107864.44 };
    static const int DaysSinceDebited[8] = { 20, 35, 2, 14, 5, 360, 1, 45 };
    static const int AccountNumbers[8] = { 1001, 7940, 4382, 2651, 3020, 7168, 6245, 9342 };
    int interest = 0;
    if (AccountNumbers[i] == 1001 || AccountNumbers[i] == 4382 || AccountNumbers[i] == 3020 || AccountNumbers[i] == 6245)
        interest = (Balance[i] * 0.06);
    else
        interest = (Balance[i] * 0.03);
    cout << AccountNumbers[i] << "\t\t" << Balance[i] << "\t\t" << DaysSinceDebited[i] << "\t" << interest << "\t" << endl;
}
0

my compiler (GCC 4.9.2) , CalcInterest AccountNumber, AccountNumber, , .

0

AccountNumber is int, and you are trying to dereference a function as AccountNumber [I]. You need to pass an int array or int pointer.

Example:

float CalcInterest(int *) 

When called:

CalcInterest(AccountNumber)
0
source

Source: https://habr.com/ru/post/1627631/


All Articles