การสร้างฟังก์ชันวนรอบเพื่อคำนวณต้นทุน

โอเค ฉันยังใหม่กับเรื่องนี้มาก และฉันกำลังพยายามสร้างโปรแกรมที่จะคำนวณค่าโทรทางไกลจำนวนเท่าใดก็ได้ ฉันยังไปได้ไม่ไกลนัก ฉันติดอยู่กับการพยายามหาวิธีให้ฟังก์ชันทำซ้ำตัวเอง ตอนนี้ฉันได้รับข้อผิดพลาดที่บอกว่า

บรรทัดที่ 18 ไม่อนุญาตให้ใช้คำจำกัดความของฟังก์ชันที่นี่ก่อนโทเค็น '{' และคาดหวัง ',' หรือ ';' ก่อนโทเค็น '{'

บรรทัดที่ 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;
}

person user2040308    schedule 04.02.2014    source แหล่งที่มา
comment
คุณมีฟังก์ชันที่ประกาศไว้ในฟังก์ชัน ย้ายวงเล็บปีกกาของคุณ นอกจากนี้ยังใช้การเรียกซ้ำได้ดีสำหรับโปรแกรมเมอร์ใหม่   -  person Will Custode    schedule 04.02.2014
comment
C ไม่อนุญาตให้ใช้ฟังก์ชันที่ซ้อนกัน ตามที่ @WilliamCustode ชี้ให้เห็น - ย้ายฟังก์ชัน costCalc() ของคุณขึ้นไปด้านบน main()   -  person twalberg    schedule 05.02.2014
comment
นี่คือ C เลยเหรอ? ฉันไม่เคยเห็น using namespace std; ทั้ง cin หรือ cout ในแหล่งที่มา C ใดๆ เลย... (หากฉันเข้าใจผิด ฉันก็ยังเป็นมือใหม่ C เช่นกัน)   -  person core1024    schedule 05.02.2014
comment
@ core1024 คุณพูดถูก ไม่ใช่ C; มันคือ C++ ไม่รองรับสตรีมใน C   -  person Stephen Rasku    schedule 05.02.2014


คำตอบ (2)


คุณควรปิดฟังก์ชันหลักก่อนที่จะเริ่มฟังก์ชันใหม่

คุณกำลังพยายามสร้างฟังก์ชันที่ซ้อนกันภายใน main และ C หรือ C++ ไม่รองรับสิ่งนั้น

คุณควรคัดลอก/วางโค้ดของฟังก์ชัน costCalc ก่อนเริ่มฟังก์ชันหลัก

ตามหมายเหตุด้านข้าง เมื่อนานมาแล้ว (1994) gcc (2.95.2) รองรับฟังก์ชันที่ซ้อนกันเป็นส่วนขยาย แต่มันไม่เคยรวมอยู่ในมาตรฐาน C หรือ C++

ฉันไม่แน่ใจแน่ชัดว่าเหตุใดฟังก์ชันที่ซ้อนกันจึงไม่เข้ามาตรฐาน มันเรียบง่ายพอที่จะรองรับได้ (เราอาจจะต้องทำให้ฟังก์ชันที่ซ้อนกันแบบอินไลน์คงที่ แต่อย่างไรก็ตาม) อาจเป็นเพราะมันอาจทำให้เกิดปัญหาเกี่ยวกับตัวเชื่อมโยงและหน่วยการคอมไพล์

นั่นไม่ใช่เรื่องใหญ่จริงๆ และกรณีการใช้งานส่วนใหญ่สามารถแก้ไขได้โดยใช้เนมสเปซ

person kriss    schedule 05.02.2014

นี่จะทำให้คุณไปแสดงต่อไป

    #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
person Community    schedule 05.02.2014
comment
คุณอาจหมายถึง delete. แต่จำเป็นต้องใช้การจัดสรรแบบไดนามิกเลยหรือไม่ หรือแม้แต่ชั้นเรียน? - person François Moisan; 05.02.2014
comment
คำถามต้องมีการยกตัวอย่างในชั้นเรียน (คล้ายกับการบ้าน...) และมีวัตถุประสงค์เชิงวาทศิลป์สำหรับผู้ถาม - person ; 05.02.2014
comment
ฉันเห็น. ฉันพลาดส่วนนั้น - person François Moisan; 05.02.2014
comment
เพิ่มการลบ(thisCall); - person ; 05.02.2014