2016-09-15 74 views
-4
/************************************************************************************** 
* The program should display each employee number         * 
* ask the user to enter that employee’s hours and pay rate.       * 
* It should then calculate the gross wages for that employee(hours times pay rate) * 
* store them in the wages array.             * 
* After the data has been entered for all the employees,       * 
* the program should display each employee’s identification number and gross wages. * 
**************************************************************************************/ 

#include <cstdlib> 
#include <iostream> 
#include <iomanip> 

using namespace std; 


const int Num_Employees = 7; // global constant of # of employees 

int empId[Num_Employees] = {5658846, 4520125, 785122, 877541, 8451277, 1302850, 7580489}; // array of Employee ID #'s 
int hours[7];  // empty array of 7 possible values for employee hours 
double payRate[7]; // empty array of 7 possible values for employee pay rates 
double wages[7]; // empty array of 7 possible values for employees wages (hours * pay rate) 

void calcGrossWages(int[], double[], double[]); // calculate gross wages prototype 



int main() { 


    // Employees 
    for(int i= 0; i< Num_Employees; i++) { 

     cout << "Your ID is: " << "" << empId[i] << endl; // displays each employee # 

     cout << "How many hours did you work?"; 

     cin >> hours[i]; 



     cout << "What was your payRate?" << endl; 

     cout <<"$"; 

     cin >> payRate[i]; 
    } 

     /*Calculate the gross wages*/ 

    for(int i = 0; i < Num_Employees; i++) { 

     wages[i] = calcGrossWages(hours[i], payRate[i], wages[i]); 


    } 

    } 



//****************************************************************************** 
//* Definition of calcGrossWages function          * 
//* This function calculates the employees Wages        * 
//* Wages are calculated by the # of hours worked by each employee    * 
//* multiplied by their enter pay rate           * 
// ***************************************************************************** 

void calcGrossWages(int hours[], double payRate[], double wages[]) 
{ 
    for (int i= 0; i< Num_Employees; i++) { 

      wages[i] = hours[i] * payRate[i]; 

    } 





} 

問題:C++ /錯誤:沒有匹配的函數調用calcGrossWages()

如何通過一個數組作爲參數成函數適當地,允許一個能在一個空數組中輸入值?

爲什麼我得到一個錯誤說呼叫「calcGrossWages」

+0

'calcGrossWages(小時[I],payRate [I],工資[I]);'與調用平原'double'參數,而不是陣列。 –

回答

0

你應該通過陣列中calcGrossWages,而不是僅僅通過數組的元素,並且不需要在主循環用於調用calcGrossWages

/******************************************************************************** ****** 
* The program should display each employee number         * 
* ask the user to enter that employee’s hours and pay  rate.       * 
* It should then calculate the gross wages for that employee(hours times pay rate) * 
* store them in the wages  array.             * 
* After the data has been entered for all the  employees,       * 
* the program should display each employee’s identification number and gross wages. * 
******************************************************************************** ******/ 

#include <cstdlib> 
#include <iostream> 
#include <iomanip> 

using namespace std; 


const int Num_Employees = 7; // global constant of # of employees 

int empId[Num_Employees] = {5658846, 4520125, 785122, 877541, 8451277, 1302850,  7580489}; // array of Employee ID #'s 
int hours[7];  // empty array of 7 possible values for employee hours 
double payRate[7]; // empty array of 7 possible values for employee pay rates 
double wages[7]; // empty array of 7 possible values for employees wages ( hours * pay rate) 

void calcGrossWages(int[], double[], double[]); // calculate gross wages prototype 



int main() { 


    // Employees 
    for(int i= 0; i< Num_Employees; i++) { 

     cout << "Your ID is: " << "" << empId[i] << endl; // displays each employee # 

     cout << "How many hours did you work?"; 

     cin >> hours[i]; 



     cout << "What was your payRate?" << endl; 

     cout <<"$"; 

     cin >> payRate[i]; 
    } 

     /*Calculate the gross wages*/ 

    calcGrossWages(hours, payRate, wages); 

    } 




//****************************************************************************** 
//* Definition of calcGrossWages function          * 
//* This function calculates the employees Wages        * 
//* Wages are calculated by the # of hours worked by each employee    * 
//* multiplied by their enter pay rate           * 
// ***************************************************************************** 

void calcGrossWages(int hours[], double payRate[], double wages[]) 
{ 
    for (int i= 0; i< Num_Employees; i++) { 

      wages[i] = hours[i] * payRate[i]; 

    } 





} 
+0

謝謝我不知道爲什麼我認爲我應該運行for循環來調用該函數。 –

0

array[i]是一個數組元素,而不是整個數組沒有匹配功能;你試圖通過int s和double而不是他們的陣列。

由於數組的名字是hourspayRatewages,你這樣調用該函數:

/*Calculate the gross wages*/ 
calcGrossWages(hours, payRate, wages) 

你並不需要一個循環,因爲函數已經處理了整個陣列,並且功能不返回任何內容(結果存儲在wages)。

(而作爲一個側面說明,這些變量應該是本地的main,而不是全局性的。)

相關問題