具有不同返回类型和参数的函数
Functions with different return types and parameteres C++
我需要这个c++问题的答案,我已经对它进行了研究,但显然我错过了一些东西,我将发布我的答案到目前为止....
编写一个计算和打印工资单的程序。
用户输入是雇员的姓名、工作小时数和每小时的工资率。
必须声明三个函数:
1) 1个输入;
2)一个计算员工工资;和
3)打印工资单
用户必须在变量theEmployee
, theHoursWorked
和thePayRate
中输入员工的姓名,工作时数和小时工资率。变量employee
是string
类型,另外两个变量是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
和>>
混合在一起。
首先,getData
和printPaySlip
函数不应该返回任何东西-返回类型应该从int
更改为void
。
第二,return theEmployee, theHoursWorked, thePayRate
行是错误的,应该删除——c++不允许多个返回值,而是使用引用形参。这意味着允许该函数修改其参数。您已经正确地读取了它们,因此只需删除return
行。
第三,calculatePay
和printPaySlip
函数不需要引用,因为它们不修改参数。甚至问题说明都说它们应该有值,所以只需删除&
's。
第四,您的calculatePay
函数不正确地计算了加班时数的比率-它只是将加班时数添加到总工资中,而没有将其乘以thePay * 1.5
。
- 将可变参数函数的参数封装在类实例中
- QML 使用带有参数C++函数
- 使用可变参数函数作为模板参数
- 如何在C++中伪造虚拟可变参数函数模板?
- 为什么可变参数函数不适用于模板
- C++ std::functional 中的可变参数函数模板
- 可变参数函数指针的定义对于VxWorks spyLib来说不清楚
- 使用可变参数函数覆盖具有不同函数签名的虚函数
- 考虑引用和常量的可变参数函数包装器
- 使用可变参数函数将整数和/或整数数组放入单个 int 数组中
- 在可变参数函数中转发特定范围的参数
- 通过引用传递参数;函数返回类型是否必须为 VOID?
- 使用带有一个参数函数的递归找到数字的平方
- 可变参数函数模板不能很好地使用 std::function 作为参数
- 多个可变参数函数的单个模板参数包?
- 参数数据类型未知的可变参数函数
- 可变参数函数参数包扩展
- 使用模板可变参数函数将多个参数传递给另一个函数
- 对可变参数函数的递归调用的链接器错误
- 通过像printf这样的可变参数函数传递一个带有常量字符*转换函数的类