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

时间:2013-04-08 11:33:24

标签: c++ function types parameters return

我需要这个C ++问题的答案,我已经研究过了,但显然我错过了一些东西,我也会发布我的答案......

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

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

您必须声明三个功能:

1)一个用于输入;

2)计算员工工资;和

3)打印工资单

用户必须在变量theEmployeetheHoursWorkedthePayRate中输入员工姓名,工作小时数和每小时工资率。变量employeestring,其他两个变量属于float类型。作为theEmployee的值,theHoursWorked和thePayRate将在此函数中更改reference parameters need to be used

计算功能将收到两个表示工作小时数和小时工资率的参数,进行计算并返回员工的工资。工作时间超过40小时的员工每小时加班工资的1.5小时工资。由于函数中的参数未更改,因此它们应为值参数。该函数应返回表示薪酬的float值。

输出功能必须显示员工姓名,工作小时数,加班时数和用户输入的每小时工资率以及员工工资。对

示例:

Pink Panther的付款单

工作小时数: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;
}

2 个答案:

答案 0 :(得分: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

BTW不是你问过的问题,但是你会遇到麻烦,因为你正在混淆getline>>。有关说明,请参阅here

答案 1 :(得分:0)

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

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

第三,calculatePayprintPaySlip函数不需要引用,因为它们不修改参数。即使是问题陈述也说他们应该取值,所以只需删除&

第四,您的calculatePay函数错误地计算加班时间的比率 - 它只是将加班时数加到总薪资中,而不是乘以thePay * 1.5