具有不同返回类型和参数的函数

Functions with different return types and parameteres C++

本文关键字:参数 函数 返回类型      更新时间:2023-10-16

我需要这个c++问题的答案,我已经对它进行了研究,但显然我错过了一些东西,我将发布我的答案到目前为止....

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

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

必须声明三个函数:

1) 1个输入;

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

3)打印工资单

用户必须在变量theEmployee, theHoursWorkedthePayRate中输入员工的姓名,工作时数和小时工资率。变量employeestring类型,另外两个变量是float类型。由于employee, theHoursWorked和thePayRate的值将在此函数中更改,reference parameters need to be used .

计算函数将接收两个参数,分别表示工作小时数和小时工资率,进行计算并返回员工的工资。工作时间超过40小时的,每加班1小时支付1.5倍的工资。由于参数在函数中没有改变,所以它们应该是值参数。该函数应该返回一个float值,该值表示支付。

输出函数必须显示员工的姓名、工作时数、加班时数和用户输入的小时工资率以及员工的工资。

的例子:

粉红豹的工资单

工作时数:43.5小时

加班时间:3.5小时

时薪:R125.35

支付:R5672.09

main函数包括一个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;
}

在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

顺便说一句,不是你问的问题,但你会有麻烦,因为你把getline>>混合在一起。

首先,getDataprintPaySlip函数不应该返回任何东西-返回类型应该从int更改为void

第二,return theEmployee, theHoursWorked, thePayRate行是错误的,应该删除——c++不允许多个返回值,而是使用引用形参。这意味着允许该函数修改其参数。您已经正确地读取了它们,因此只需删除return行。

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

第四,您的calculatePay函数不正确地计算了加班时数的比率-它只是将加班时数添加到总工资中,而没有将其乘以thePay * 1.5