繁体   English   中英

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

[英]Functions with different return types and parameteres C++

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

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

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

您必须声明三个函数:

1)一个输入;

2)一种计算员工工资的方法;

3)一张打印工资单

用户必须在变量theEmployeetheHoursWorkedthePayRate输入雇员的姓名,工作小时数和小时工资率。 变量employee是一个string ,另外两个变量是float类型。 由于此函数中Employee,theHoursWorked和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;
}

无需在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>>混合。 请参阅此处以获取解释。

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

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

第三, calculatePayprintPaySlip函数不需要引用,因为它们不会修改参数。 甚至问题陈述说它们也应该采用值,因此只需删除&即可。

四,您calculatePay函数计算加班时间率不正确-它只是增加了加班时间的总支付的金额不乘以它thePay * 1.5

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM