使用函数返回值

Using a function to return a value

本文关键字:返回值 函数      更新时间:2023-10-16

我目前有这个代码,它可以工作并根据设置的条件计算帐户的利息。但是,我现在需要编写一个名为 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 Numbert" << "Balancett" << "Dayst" << "Interestt" << 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] << "tt" << Balance[i] << "tt" << DaysSinceDebited[i] << "t" << interest << "t" << endl;
    }
    system("pause");
    return 0;
}

这是我尝试的,该功能不起作用,但没有错误

   #include <iostream>
 using namespace std;

 float CalcInterest(int AccountNum);

int main()
{

cout << "Account Numbert" << "Balancett" << "Dayst" << "Interestt" << 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] << "tt" << Balance[i] << "tt" << DaysSinceDebited[i] << "t" << interest << "t" << endl;
}


}

首先,一个严重的语法错误可能是由代码的复制和粘贴引起的:

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

如果你(1(打算调用函数CalcInterest(),你写下它的名字,然后在圆括号内写下实际参数,如下所示:

CalcInterest(5);
但是,如果要声明函数,

语法本身是正确的,但函数声明不属于另一个函数(不包括完全在另一个代码块中定义的 lambda(。有趣的是,据我了解,这样编译就可以了:声明类型为 float 的变量CalcInterest,并将默认构造int传递给其构造函数,称为 AccountNumber。我说的对吗?应用C++语法的强大功能。

你的第二个错误是这样的:

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

在你(重新(声明AccountNumber为长度8int数组后,你的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 Numbert" << "Balancett" << "Dayst" << "Interestt" <<    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] << "tt" << Balance[i] << "tt" << DaysSinceDebited[i] << "t" << interest << "t" << endl;
}

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

帐号是 int,您正在尝试在函数中取消引用为帐号[i].您需要传递 int 数组或 int 指针。

例:

float CalcInterest(int *) 

呼叫时:

CalcInterest(AccountNumber)