简体   繁体   English

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

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

I need an answer for this C++ question, I have worked on it, but clearly im missing something and I will post my answer so far too.... 对于这个C ++问题,我需要一个答案,我已经在解决它,但是显然我错过了一些东西,到目前为止我也将发布我的答案...。

Write a program that will calculate and print pay slips. 编写一个程序来计算和打印工资单。

User inputs are the name of the employee, the number of hours worked and the hourly pay rate. 用户输入是员工的姓名,工作小时数和小时工资率。

You have to declare three functions: 您必须声明三个函数:

1) one for input; 1)一个输入;

2) one to calculate the employees pay; 2)一种计算员工工资的方法; and

3) one to print the payslip 3)一张打印工资单

The user has to input the name of the employee, the number of hours worked and the hourly pay rate into the variables theEmployee , theHoursWorked and thePayRate . 用户必须在变量theEmployeetheHoursWorkedthePayRate输入雇员的姓名,工作小时数和小时工资率。 The variable employee is a string and the other two variables are of the type float . 变量employee是一个string ,另外两个变量是float类型。 As the values of theEmployee, theHoursWorked and thePayRate will be changed in this function, reference parameters need to be used . 由于此函数中Employee,theHoursWorked和PayRate的值将更改,因此reference parameters need to be used

The calculation function will receive two parameters that represent the number of hours worked and the hourly pay rate, do the calculation and return the pay for the employee. 计算功能将接收两个参数,分别代表工作小时数和小时工资率,进行计算并返回员工的工资。 An employee who has worked more than 40 hours is paid 1.5 times the hourly pay rate for each hour of the overtime. 工作时间超过40小时的雇员的每小时加班时薪是小时工资率的1.5倍。 As the parameters are not changed in the function, they should be value parameters. 由于参数未在函数中更改,因此它们应为值参数。 The function should return a float value which represents the pay. 该函数应返回一个代表薪水的float值。

The output function has to display the name of the employee, the number of hours worked, the number of overtime hours and the hourly pay rate entered by the user as well as the employee's pay. 输出功能必须显示雇员的姓名,工作小时数,加班小时数,用户输入的小时工资率以及雇员的工资。 for 对于

Example: 例:

Pay slip for Pink Panther 粉红豹的工资单

Hours worked: 43.5 hours 工作时间:43.5小时

Overtime hours: 3.5 hours 超时时间:3.5小时

Hourly pay rate: R125.35 每小时工资率:R125.35

Pay: R5672.09 薪水:R5672.09

The main function includes a for loop that allows the user to repeat the calculation of a pay slip for five employees. 主要功能包括一个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;
}

THATS WHAT THEY GAVE, this is what I have done so far, I guess that I am struggling with the reference parameters? 那是什么,到目前为止,这是我所做的,我想我在参考参数方面很挣扎?

#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;
}

No need for return in the getData functrion. 无需在getData函数中返回。 This should work 这应该工作

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;

}

Note the getData function is declared void, which means strictly speaking it does not return a value. 请注意,getData函数被声明为void,这意味着严格来说它不会返回值。 In your problem spec 'return' is being used loosely to mean a value which a function calculates and then gives back to the calling function, it's doesn't mean you have to have an actual return in your code. 在您的问题规范中,“ return”被宽松地用来表示一个值,该值由函数计算然后返回给调用函数,但这并不意味着您必须在代码中实际return

BTW not the qestion you asked but you are going to have trouble because you are mixing getline with >> . 顺便说一句,不是您问的问题,但是您将遇到麻烦,因为您将getline>>混合。 See here for an explanation. 请参阅此处以获取解释。

First, getData and printPaySlip functions shouldn't return anything - return type should be changed from int to void . 首先, getDataprintPaySlip函数不应返回任何内容-返回类型应从int更改为void

Second, the line return theEmployee, theHoursWorked, thePayRate is an error and should be removed - C++ doesn't allow multiple return values, and uses reference parameters instead. 其次,该行return theEmployee, theHoursWorked, thePayRate是一个错误,应删除-C ++不允许多个返回值,而是使用引用参数。 This means that the function is allowed to modify its parameters. 这意味着允许该函数修改其参数。 You already read them correctly, so simply remove the return line. 您已经正确阅读了它们,因此只需删除return线即可。

Third, the calculatePay and printPaySlip functions do not require references, as they do not modify the parameters. 第三, calculatePayprintPaySlip函数不需要引用,因为它们不会修改参数。 Even the problem statement says they should take values, so simply remove the & 's. 甚至问题陈述说它们也应该采用值,因此只需删除&即可。

Fourth, your calculatePay function calculates the rate for overtime hours incorrectly - it simply adds the amount of overtime hours to the total pay without multiplying it by thePay * 1.5 . 四,您calculatePay函数计算加班时间率不正确-它只是增加了加班时间的总支付的金额不乘以它thePay * 1.5

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

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