创建循环功能以计算成本
Creating a loop function to calculate cost
好的,所以我非常新手,我正在尝试创建一个程序,该程序将计算任何数量的长距离调用的成本。我还没有走得很远,我坚持不懈地试图弄清楚如何使功能重复。现在我遇到一个错误说
第18行,在" {'{'token and Expect''或';'之前,不允许在此处使用功能定义。在'{'token。
之前
第18行是直接 void costCalc(int numCalls)
这是我到目前为止的代码:
#include<iostream>
using namespace std;
int main()
{
// Declare and initialize all variables
int numCalls = 0;
int length = 0;
int hour = 0;
char day = ' ';
char dest = ' ';
double cost = 0.0;
cout<<"Enter the number of calls: ";
cin>>numCalls;
void costCalc(int numCalls)
{
if (numCalls > 0)
{
cout<<"Enter length of call in minutes: ";
cin>>length;
costCalc(numCalls-1);
}
}
// Request the number of calls from the user
// Loop for the requested number of calls:
// Request the user to give you the call length,
// call day of week and hour of call, and call
// destination
// Instantiate and initialize a Call object
// using either
// a) the 4-parameter constructor, OR
// b) the default constructor and each of the
// set member functions.
// Get the cost of the call using the calcCallCost
// function and add it to the total cost of the calls.
// Use the callMsg function to print a message about the call
// end loop
// Report the total cost of all the calls.
system("pause");
return 0;
}
您应该在开始新的功能之前关闭主函数。
您正在尝试在main内部创建一个嵌套功能,而C或C 不支持。
您应该仅在主函数开始之前复制/粘贴CostCalc功能的代码。
作为旁注,很久以前(1994)GCC(2.95.2)支持嵌套功能作为扩展。但是它从未进入C或C 标准。
我不确定为什么嵌套功能永远不会进入标准。它的接缝非常简单以支撑(我们可能必须使嵌套的函数内联静态,但仍然是)。也许是因为它会提出一些链接和编译单元问题。
实际上并不重要,大多数用例都可以使用名称空间解决。
这将使您继续参加演出。
#include <iostream>
#include <iomanip> // for setprecision(2)
using namespace std;
class Call {
private: double length;
public:
// Instantiate and initialize a Call object
// using either
// a) the 4-parameter constructor, OR
Call(double theLength)
{
this->length = theLength;
}
// b) the default constructor and each of the
// set member functions.
Call() { }
void SetLength(double someLength)
{
this->length = someLength;
}
double GetLength()
{
return this->length;
}
};
double costCalc(double costPerMinute, int length)
{
Call thisCall(length);
return thisCall.GetLength()*costPerMinute;
}
void callMsg()
{
cout << "This is a message about the call" << endl;
}
int main()
{
int numCalls = 0;
int length = 0;
double cost = 0.0;
double costPerMinute = 0.05;
// Request the number of calls from the user
cout << "Enter the number of calls: ";
cin >> numCalls;
// Loop for the requested number of calls:
for (int i = 0; i < numCalls; i++)
{
// Request the user to give you the call length,
cout << "Enter length of call " << i << " in minutes: ";
cin >> length;
// Get the cost of the call using the calcCallCost
// function and add it to the total cost of the calls.
cost += costCalc(costPerMinute, length);
// Use the callMsg function to print a message about the call
callMsg();
} // end loop
// Report the total cost of all the calls.
cout << "Total cost is $" << setprecision(2) << cost << endl;
return 0;
}
编译/运行:
g++ cost.cpp
$ ./a.out
Enter the number of calls: 2
Enter length of call 0 in minutes: 1
This is a message about the call
Enter length of call 1 in minutes: 2
This is a message about the call
Total cost is $0.15
相关文章:
- 当 A 在 for 循环中调用函数 B 时,如何计算函数 A 的空间复杂度?
- 如何编写计算功率的循环
- 在循环条件 i<sqrtN(预先计算)和 i*i<N 中,C++哪个更有效?
- 如果代码包含多个复杂度循环,如何计算复杂度
- C++在不使用pow或循环的情况下计算一个数字的幂
- 计算递归和时的无限循环
- 使用与 openmp C++并行的循环计算矩阵中每一行的最小值
- getline() 在 while 循环条件中如何计算为真或假?
- 为什么 g++ 将计算拉入热循环
- 计算使用 C++ std 函数而不是 for 循环的大 O
- 无需使用循环,就将ISBN-10的前9位数字读取为字符串并计算第10位数字
- 将多个计算放入使用基于迭代次数的变量的 for 循环中
- C 平均程序在循环时不计算n后不计算平均值
- 计算用户使用循环输入的整数数量
- 此递归中发生了什么?我需要仅使用递归/无循环来计算和备份
- 冰雹序列C 函数需要计算序列的长度.无限循环问题
- 在游戏更新循环中计算布尔的真/假变化
- 如何计算循环中正确答案的数量
- 计算一个表达式来计算循环
- 如何计算循环执行了多少次?C++