用一个函数返回一个值 [英] Using a function to return a value

查看:170
本文介绍了用一个函数返回一个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在的作品,并计算根据条件设置的​​帐户的利息眼前这个code。不过,我现在需要 CalcInterest()名为codeA功能,作为其的只有的参数 - 帐户 - 并返回利息计算

 的#include<&iostream的GT;使用命名空间std;诠释的main()
{    INT账户号码[8] = {1001,7940,4382,2651,3020,7168,6245,9342};    浮动平衡[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利息= 0;    //此处添加您的code    COUT<< 帐号\\ t的<< 平衡\\ t \\ t的<< 天\\ t的<< 利息\\ t的<< ENDL;
    的for(int i = 0; I< 8;我++)
    {
        如果(余额[Ⅰ]≥10000 || DaysSinceDebited [Ⅰ]→30)
            利息=(平衡[I] * 0.06);
        其他
            利息=(平衡[I] * 0.03);        COUT<<账户号码[1] - ;&下; \\ t \\ t的<<平衡[1] - ;< \\ t \\ t的<< DaysSinceDebited [1] - ;&下; \\ t的<<兴趣与LT;< \\ t的<< ENDL;
    }    系统(暂停);
    返回0;
}

下面是我尝试,该功能无法正常工作,但没有任何错误

 的#include<&iostream的GT; 使用命名空间std;
 浮CalcInterest(INT AccountNum);诠释的main(){COUT<< 帐号\\ t的<< 平衡\\ t \\ t的<< 天\\ t的<< 利息\\ t的<< ENDL;浮CalcInterest(INT AccountNum);系统(暂停);
返回0;
};浮CalcInterest(INT AccountNum){INT利息= 0;
浮动平衡[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账户号码[8] = {1001,7940,4382,2651,3020,7168,6245,9342};
的for(int i = 0; I< 8;我++)
{
如果(余额[Ⅰ]≥10000 || DaysSinceDebited [Ⅰ]→30)
返回利息=(平衡[I] * 0.06);
其他
返回利息=(平衡[I] * 0.03);
COUT<<账户号码[1] - ;&下; \\ t \\ t的<<平衡[1] - ;< \\ t \\ t的<< DaysSinceDebited [1] - ;&下; \\ t的<<兴趣与LT;< \\ t的<< ENDL;
}}


解决方案

首先,可能是由code复制和粘贴造成了严重的语法错误:

  INT的main()
{
    / * ... * /
    浮CalcInterest(INT账户号码);
    / * ... * /
};

如果你(1)打算的呼叫的功能 CalcInterest(),你写它的名字,然后轮括号喜欢里面的实际参数所以:

  CalcInterest(5);

不过,若你想的声明的功能,语法是正确的本身,而是函数声明不里面的另一个功能(短lambda表达式被$ C $的另一个块内完全确定归属。ç有趣的是,这个编译好,据我了解,这种方式:声明变量 CalcInterest 类型浮动,并传递给它的构造函数构造一个默认的 INT 名为账户号码 的。我说得对不对? C ++语法的力量应用。

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

 浮动CalcInterest(INT账户号码){
    / * ... * /
    INT账户号码[8] = {1001,7940,4382,2651,3020,7168,6245,9342};
    如果(账户号码== 1001 / * ... * /){/ * ... * /}

在(再)后申报账户号码作为长度 INT 阵列 8 ,您 INT账户号码函数参数被遮蔽,因此编译器认为你要比较 1001 到阵列 INT账户号码[8] 。这里所说的错误:

  main.cpp中:在功能上浮动CalcInterest(INT):
main.cpp中:21:24:错误:的'廉政账户号码[8]'声明阴影参数
     INT账户号码[8] = {1001,7940,4382,2651,3020,7168,6245,9342};
                        ^
main.cpp中:33:30:​​错误:ISO C ++禁止指针和整数之间的比较[-fpermissive]
         如果(账户号码== || 1001 ==账户号码4382 ||账户号码== || 3020 ==账户号码6245)
                              ^

您第三个错误,这个时候的逻辑,都是在的回报循环。想想看,在第一个循环迭代的第一个收益即将OT得到执行,而不是其他的函数内将运行!这就是你打算做什么?

最后,据我了解,你要分解出code在for循环到一个函数吧?也许像这样进行:

 的#include<&iostream的GT;使用命名空间std;无效CalcInterest(INT I);诠释的main()
{
    COUT<< 帐号\\ t的<< 平衡\\ t \\ t的<< 天\\ t的<< 利息\\ t的<< ENDL;
    对于(INT账户= 0;账户< 8;帐户++){
        CalcInterest(账户);
    }
    系统(暂停);
    返回0;
};无效CalcInterest(int i)以{
    静态常量浮动平衡[8] = {4254.40,27006.25,123.50,85326.92,657.0,7423.34,4.99,107864.44};
    静态const int的DaysSinceDebited [8] = {20,35,2,14,5,360 1,45};
    静态const int的AccountNumbers [8] = {1001,7940,4382,2651,3020,7168,6245,9342};
    INT利息= 0;
    如果(AccountNumbers [I] == || 1001 AccountNumbers [I] == || 4382 AccountNumbers [I] == || 3020 AccountNumbers [I] == 6245)
        利息=(平衡[I] * 0.06);
    其他
        利息=(平衡[I] * 0.03);
    COUT<< AccountNumbers [1] - ;&下; \\ t \\ t的<<平衡[1] - ;< \\ t \\ t的<< DaysSinceDebited [1] - ;&下; \\ t的<<兴趣与LT;< \\ t的<< ENDL;
}

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





}

解决方案

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

这篇关于用一个函数返回一个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆