0

このC ++の質問に対する回答が必要です。私はそれに取り組んでいますが、明らかに何かが欠けているので、これまでの回答も投稿します....

給与明細を計算して印刷するプログラムを作成します。

ユーザー入力は、従業員の名前、勤務時間数、および時給です。

次の 3 つの関数を宣言する必要があります。

1) 入力用。

2) 従業員の給与を計算するもの。と

3) 給与明細を印刷する人

theEmployeeユーザーは、従業員の名前、勤務時間数、および時給を変数theHoursWorkedおよびに入力する必要がありthePayRateます。変数employeeは astringで、他の 2 つの変数は型floatです。theEmployee、theHoursWorked、thePayRate の値がこの関数で変更されるため、reference parameters need to be used.

計算関数は、勤務時間数と時給を表す 2 つのパラメーターを受け取り、計算を行い、従業員の給与を返します。40 時間を超えて働いた従業員には、残業 1 時間ごとに時給の 1.5 倍の賃金が支払われます。パラメータは関数内で変更されないため、値パラメータにする必要があります。floatこの関数は、支払いを表す値を返す必要があります。

出力関数は、従業員の名前、労働時間数、残業時間数、ユーザーが入力した時給、および従業員の給与を表示する必要があります。為に

例:

ピンクパンサーの給与明細

労働時間:43.5時間

残業時間:3.5時間

時給: R125.35

支払う: R5672.09

メイン関数には、ユーザーが 5 人の従業員の給与明細の計算を繰り返すことができる 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;
}
4

2 に答える 2

0

まず、getData関数printPaySlipは何も返すべきではありません。戻り値の型を からintに変更する必要がありますvoid

次に、この行return theEmployee, theHoursWorked, thePayRateはエラーであり、削除する必要があります。C++ は複数の戻り値を許可せず、代わりに参照パラメーターを使用します。これは、関数がそのパラメーターを変更できることを意味します。すでに正しく読んでいるので、単にreturn行を削除してください。

第 3 に、関数calculatePayprintPaySlip関数はパラメーターを変更しないため、参照を必要としません。&問題のステートメントでさえ、値を取るべきだと言っているので、単純に'sを削除してください。

第 4 に、あなたのcalculatePay関数は残業時間のレートを間違って計算します。これは、残業時間の金額に を掛けずに、合計給与に加算するだけthePay * 1.5です。

于 2013-04-08T11:44:14.430 に答える
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 関数は void と宣言されていることに注意してください。これは、厳密に言えば、値を返さないことを意味します。問題の仕様では、関数が計算して呼び出し元の関数に返す値を意味するために「戻り値」が大まかに使用されていますがreturn、コードに実際の値が必要であるという意味ではありません。

ところで、あなたが尋ねた質問ではありませんが、 と混合getlineしているため、問題が発生するでしょう>>説明については、こちらを参照してください。

于 2013-04-08T11:38:20.053 に答える