使用函数返回值
Using a function to return a value
我目前有这个代码,它可以工作并根据设置的条件计算帐户的利息。但是,我现在需要编写一个名为 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
为长度8
的int
数组后,你的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)
- 从python中调用C++函数并获取返回值
- 编译器警告:执行到达值返回函数的末尾而不返回值
- 将返回值存储在函数指针数组的指针中是如何工作的?
- 程序不向函数返回值
- 如何在另一个函数中使用返回值作为参数?
- 编写以 C++ 为单位返回值的函数
- 了解在返回值之前和之后使用 EAX 的函数调用
- 当我使用 void 函数的返回值(通过强制转换函数指针)时,究竟会发生什么?
- 如何在不使用临时变量的情况下取消引用返回指针的函数的返回值?
- 如何将不可移动和不可复制的函数返回值获取到数组中
- G++ 编译器是否在未使用返回值的情况下将 constexpr 函数视为常规函数?
- 为什么当你将函数的返回值乘以零时它不会短路?
- 函数的返回值类型是别名 ***或 *** 布尔值
- 如何在没有返回值的情况下使用(特征)unaryExpr 和 lambda 函数?
- 创建 std::函数,它返回具有函数成员值的变量.分段错误
- 为什么这个"ctypes"包装函数的返回值是"c_long(0)"而不是&quo
- 函数不显示返回值 - C++
- 结构新手,我对如何从 void 函数中返回值并将其放入另一个函数感到困惑
- 将 void 函数更改为返回值函数
- 返回值函数中结构成员赋值的额外拷贝