0

我需要这个 C++ 问题的答案,我已经解决了,但显然我错过了一些东西,到目前为止我也会发布我的答案......

编写一个程序来计算和打印工资单。

用户输入的是员工姓名、工作小时数和小时工资率。

您必须声明三个函数:

1) 一个用于输入;

2) 一、计算员工工资;和

3)一张打印工资单

用户必须在变量和中输入员工姓名theEmployee、工作小时数和小时工资率。变量是 a ,其他两个变量的类型是。由于 theEmployee、theHoursWorked 和 thePayRate 的值将在此函数中更改,.theHoursWorkedthePayRateemployeestringfloatreference 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;
}
4

2 回答 2

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”被松散地用于表示函数计算然后返回给调用函数的值,这并不意味着您必须return在代码中有实际值。

顺便说一句,不是你问的问题,但你会遇到麻烦,因为你正在getline>>. 请参阅此处以获取解释。

于 2013-04-08T11:38:20.053 回答
0

首先,getData函数printPaySlip不应该返回任何东西——返回类型应该从 更改intvoid

其次,该行return theEmployee, theHoursWorked, thePayRate是一个错误,应该删除 - C++ 不允许多个返回值,而是使用引用参数。这意味着允许该函数修改其参数。您已经正确阅读了它们,因此只需删除该return行。

第三,calculatePayandprintPaySlip函数不需要引用,因为它们不修改参数。甚至问题陈述都说它们应该取值,所以只需删除&'s.

第四,您的calculatePay函数错误地计算了加班时间的费率 - 它只是将加班时间加到总工资中,而没有将其乘以thePay * 1.5

于 2013-04-08T11:44:14.430 回答