简体   繁体   English

使用函数返回值

[英]Using a function to return a value

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. 但是,我现在需要编写一个名为CalcInterest()的函数,该函数将其唯一参数-一个CalcInterest()并返回计算出的利息。

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





}

我的编译器(GCC 4.9.2)说过,您的函数CalcInterest有一个名为AccountNumber的参数,在此函数中,您也有一个名为AccountNumber的数组,在我重命名任何一个之后,都没有问题。

The AccountNumber is int and you are trying to dereference in the function as AccountNumber[i]. AccountNumber为int,并且您正在尝试在作为AccountNumber [i]的函数中取消引用。 You need to pass int array or int pointer. 您需要传递int数组或int指针。

Example: 例:

float CalcInterest(int *) 

When calling: 致电时:

CalcInterest(AccountNumber)

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: 如果您(1)打算调用函数CalcInterest() ,请先编写其名称,然后在圆括号内输入实际参数,如下所示:

CalcInterest(5);

If you however want to declare 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. 但是,如果您要声明函数,则语法本身是正确的,但是函数声明不属于另一个函数(缺少在另一个代码块中完全定义的lambda)。有趣的是,这种编译方式可以达到我这样理解: 声明float类型的变量CalcInterest并将其默认构造的int传递给其构造函数AccountNumber 。是吗?

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] . 在将AccountNumber声明为长度为8int数组之后,您的int AccountNumber函数参数将被屏蔽,因此编译器认为您要将1001int 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. 您的第三个错误(这次是合乎逻辑的)是for循环内的返回。 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! 想想看,在第一个循环迭代中,第一个return将执行ot,该函数内部将无其他运行! 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? 最后,据我了解,您想将for循环中的代码分解为一个函数,对吗? 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM