2013-04-08 79 views
0

我需要爲這個℃的答案++的問題,我已經在它的工作,但很明顯,我在想念的東西,我會後我的答案迄今太....功能具有不同的返回類型和parameteres C++

寫計算並打印工資單。

用戶輸入是員工姓名,工作小時數和小時工資率。

你必須聲明三個功能:

1)一個用於輸入;

2)一個用於計算員工工資;和

3)一個打印工資單

用戶必須輸入員工的姓名,工作小時數和小時工資率成變量theEmployeetheHoursWorkedthePayRate。變量employeestring,其他兩個變量的類型爲float。作爲員工的價值觀,小時工和payRate將在此功能中更改,reference parameters need to be used

計算功能將接收兩個參數,這兩個參數表示工作小時數和小時工資率,進行計算並返回員工的工資。工作時間超過40小時的員工每小時加班工資的時薪爲工資的1.5倍。由於參數在函數中沒有改變,它們應該是值參數。該函數應該返回代表薪水的float值。

輸出功能必須顯示員工的姓名,工作的小時數,加班時間的數量和用戶輸入的小時工資率以及員工的工資。對於

例子:

的工資條爲粉紅豹

工時:43.5小時

加班時間:3.5小時

小時工資率:R125.35

收費:R5672.09

主要功能包括一個for循環,允許用戶重複計算五名員工的工資單。

int main() 
{ 
string theEmployee; 
float theHoursWorked; 
float thePayRate; 
int thePay; 

for (int i = 0; i < 5; i++) 
{ 
    getData(theEmployee, theHoursWorked, thePayRate); 
    thePay = calculatePay (theEmployee, theHoursWorked, thePayRate); 
    printPaySlip(theEmployee, theHoursWorked, thePayRate, thePay); 
} 

return 0; 
} 

這是什麼他們得到,這是我迄今爲止所做的,我想我正在努力與參考參數?

#include <iostream> 
#include <string> 

using namespace std; 

int getData (string & theEmployee , float & theHoursWorked, float & thePayRate) 
{ 
cout<< "Enter your name and surname: "<< endl; 
getline(cin, theEmployee); 

cout << "Include the numbers of hours you worked: " << endl; 
cin >> theHoursWorked; 

cout << "What is your hourly pay rate?" << endl; 
cin >> thePayRate; 

return theEmployee, theHoursWorked, thePayRate; 

} 

float calculatePay(string & theEmployee, float & theHoursWorked, float & thePayRate) 
{ 
float tempPay, thePay, overtimeHours; 
if (theHoursWorked > 40) 
    { 
    tempPay = 40 * thePayRate; 
    overtimeHours = theHoursWorked - 40; 
    thePay = tempPay + overtimeHours;} 
else 
    thePay = theHoursWorked * thePayRate; 
    return thePay; 
} 

int printPaySlip(string & theEmployee, float & theHoursWorked, float &  
thePayRate, float thePay) 
{ 
float overtimeHours; 
cout << "Pay slip for " << theEmployee <<endl; 
cout << "Hours worked: "<< theHoursWorked << endl; 
if (theHoursWorked > 40) 
    overtimeHours = theHoursWorked - 40; 
else 
    overtimeHours = 0; 
cout << "Overtime hours: "<< overtimeHours << endl; 
cout << "Hourly pay rate: " << thePayRate << endl; 
cout << "Pay: " << thePay << endl; 
cout << endl; 

} 


int main() 
{ 
string theEmployee; 
float theHoursWorked; 
float thePayRate; 
int thePay; 

for (int i = 0; i < 5; i++) 
{ 
    getData(theEmployee, theHoursWorked, thePayRate); 
    thePay = calculatePay (theEmployee, theHoursWorked, thePayRate); 
    printPaySlip(theEmployee, theHoursWorked, thePayRate, thePay); 
} 

return 0; 
} 
+1

此外,您並沒有正確計算加班時間的工資率 - 您應該將問題描述的工資率倍數乘以1.5倍,而不是簡單地將其加到工資中。 – riv 2013-04-08 11:40:38

回答

0

無需返回getData函數。這應該工作

void getData (string & theEmployee , float & theHoursWorked, float & thePayRate) 
{ 
    cout<< "Enter your name and surname: "<< endl; 
    getline(cin, theEmployee); 

    cout << "Include the numbers of hours you worked: " << endl; 
    cin >> theHoursWorked; 

    cout << "What is your hourly pay rate?" << endl; 
    cin >> thePayRate; 

} 

注意的getData函數被宣佈無效,這意味着嚴格來說,它沒有返回值。在你的問題規範中,'return'被鬆散地用來表示函數計算的值,然後返回給調用函數,但這並不意味着你的代碼中必須有實際的return

順便說一句,不是你問的問題,但你會遇到麻煩,因爲你在混合getline>>。有關說明,請參閱here

+0

函數'printPaySlip'應該返回void。 – sobol6803 2013-04-08 11:43:07

+0

嗨 非常感謝您提供豐富的反饋信息,但是如果您編譯並運行它,您將會看到它仍然不會讓我輸入第二輪名稱和內容!我不明白。之後你做了第一輪的值和東西它結束並重新開始,但在第三行,跳過名稱和小時?:( – user2257336 2013-04-08 15:20:38

+0

@ user2257336正如我在我的答案中說,這是因爲你正在將getline與>>混合。這個問題在這裏已經有數百次的問題了,我不能回答它比已經給出的所有答案都好,請看看我上面發佈的鏈接。 – john 2013-04-08 15:36:29

0

首先,getDataprintPaySlip函數不應返回任何內容 - 返回類型應從int更改爲void

其次,行return theEmployee, theHoursWorked, thePayRate是一個錯誤,應該刪除--C++不允許多個返回值,而是使用引用參數。這意味着該功能可以修改其參數。您已經正確讀取它們,只需刪除return行即可。

第三,calculatePayprintPaySlip函數不需要引用,因爲它們不會修改參數。即使是問題陳述說他們應該取值,所以只需刪除&即可。

第四,您的calculatePay函數錯誤地計算加班時間的費率 - 它只是將加班小時數加到總費用中,而不會乘以thePay * 1.5

相關問題