Using a function to return a value

大城市里の小女人 提交于 2020-01-17 06:46:19

问题


I have this code at the moment that works and calculates the interest of an account depending on the conditions set. However I now need to code a function called CalcInterest() which takes as its only parameter - an Account, - and return the Interest calculated.

#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 have attempted, The function isn't working 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;
}





}

回答1:


First off, a grave syntax error possibly caused by copy-and-paste of code:

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

If you (1) intend to call the function CalcInterest(), you write its name and then the actual parameters inside round parens like so:

CalcInterest(5);

If you however want todeclare the function, the syntax is correct per se, but function declarations do not belong inside another functions (short of lambdas that are defined completely inside another block of code. Funny thing is, this compiles fine, as far as I understand, this way: declare variable CalcInterest of type float and pass to its constructor a default constructed int called AccountNumber. Am I right? The power of C++ syntax applied.

Your second error is this:

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

After you (re)declare AccountNumber as an array of int of length 8, your int AccountNumber function parameter gets shadowed, hence the compiler thinks you want to compare 1001 to the array int AccountNumber[8]. Here goes the error:

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)
                              ^

Your third error, this time logical, are the returns inside the for loop. think about it, on the very first loop iteration the first return is going ot get executed, and nothing else inside the function will be run! Is that what you intend to do?

Finally, as far as I understand, you want to factor out the code in the for loop into a function right? Might be done like so:

#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;
}



回答2:


my compiler (GCC 4.9.2) said your function CalcInterest has a parameter named AccountNumber and in this function you have an array named AccountNumber too, after i rename either one, no issues.




回答3:


The AccountNumber is int and you are trying to dereference in the function as AccountNumber[i]. You need to pass int array or int pointer.

Example:

float CalcInterest(int *) 

When calling:

CalcInterest(AccountNumber)


来源:https://stackoverflow.com/questions/35249551/using-a-function-to-return-a-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!