Membuat fungsi loop untuk menghitung biaya

Oke jadi saya sangat baru dalam hal ini dan saya mencoba membuat program yang akan menghitung biaya sejumlah panggilan jarak jauh. Saya belum melangkah terlalu jauh, saya terjebak dalam mencoba mencari cara agar fungsi tersebut terulang kembali. Saat ini saya mendapat pesan kesalahan

Baris 18 definisi fungsi tidak diperbolehkan di sini sebelum '{' token dan diharapkan ',' atau ';' sebelum tanda '{'.

Baris 18 adalah garis tepat setelah void costCalc(int numCalls)

Ini kode saya sejauh ini:

#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 sumber
comment
Anda memiliki fungsi yang dideklarasikan dalam suatu fungsi. Pindahkan tanda kurung kurawal Anda. Juga, penggunaan rekursi yang baik untuk programmer baru.   -  person Will Custode    schedule 04.02.2014
comment
C tidak mengizinkan fungsi bersarang, seperti yang ditunjukkan oleh @WilliamCustode - pindahkan fungsi costCalc() Anda ke atas main().   -  person twalberg    schedule 05.02.2014
comment
Apakah ini C sama sekali? Saya belum pernah melihat using namespace std; baik cin atau cout di dalam C sumber mana pun... (koreksi saya jika saya salah. Saya C pemula juga)   -  person core1024    schedule 05.02.2014
comment
@ core1024, Anda benar. Ini bukan C; itu C++. Streaming tidak didukung di C.   -  person Stephen Rasku    schedule 05.02.2014


Jawaban (2)


Anda harus menutup fungsi utama sebelum memulai yang baru.

Anda mencoba membuat fungsi bersarang di dalam main dan C atau C++ tidak mendukungnya.

Anda cukup menyalin/menempelkan kode fungsi costCalc sebelum awal fungsi utama.

Sebagai catatan tambahan, dahulu kala (1994) gcc (2.95.2) mendukung fungsi bersarang sebagai ekstensi. Tapi itu tidak pernah masuk ke standar C atau C++.

Saya tidak yakin mengapa fungsi bersarang tidak pernah masuk ke standar. Kelihatannya cukup sederhana untuk didukung (kita mungkin harus membuat fungsi bersarang sebaris menjadi statis, namun demikian). Mungkin karena itu akan menimbulkan beberapa masalah linker dan unit kompilasi.

Hal ini sebenarnya bukan masalah besar dan sebagian besar kasus penggunaan dapat diselesaikan menggunakan namespace.

person kriss    schedule 05.02.2014

Ini akan membuat Anda terus menonton pertunjukan.

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

Kompilasi/jalankan:

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
Anda mungkin bermaksud delete. Tapi apakah ada kebutuhan untuk menggunakan alokasi dinamis sama sekali? Atau bahkan satu kelas? - person François Moisan; 05.02.2014
comment
Pertanyaan tersebut memerlukan contoh kelas (menyerupai pekerjaan rumah...) dan memiliki tujuan retoris bagi penanya. - person ; 05.02.2014
comment
Jadi begitu. Saya melewatkan bagian itu. - person François Moisan; 05.02.2014
comment
Menambahkan hapus (Panggilan ini); - person ; 05.02.2014