创建循环功能以计算成本

Creating a loop function to calculate cost

本文关键字:计算 循环 功能 创建      更新时间:2023-10-16

好的,所以我非常新手,我正在尝试创建一个程序,该程序将计算任何数量的长距离调用的成本。我还没有走得很远,我坚持不懈地试图弄清楚如何使功能重复。现在我遇到一个错误说

第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