【问题标题】:Functions with different return types and parameteres C++具有不同返回类型和参数的函数 C++
【发布时间】:2013-04-08 11:33:23
【问题描述】:

我需要这个 C++ 问题的答案,我已经解决了,但显然我遗漏了一些东西,到目前为止我也会发布我的答案......

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

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

你必须声明三个函数:

1) 一个用于输入;

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

3) 一张打印工资单

用户必须在变量theEmployeetheHoursWorkedthePayRate中输入员工姓名、工作小时数和小时工资率。变量employeestring,另外两个变量的类型是float。由于 theEmployee、theHoursWorked 和 thePayRate 的值将在此函数中更改,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;
}

【问题讨论】:

  • 此外,您没有正确计算加班时间的工资率 - 您应该将 overtimeHours 乘以工资率乘以 1.5,而不是简单地将其添加到工资中。

标签: c++ function types parameters return


【解决方案1】:

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&gt;&gt; 混合在一起。请参阅here 了解说明。

【讨论】:

  • 同样函数printPaySlip应该返回void。
  • 您好非常感谢您提供的信息反馈,但它仍然不允许我输入第二轮名称和内容,如果您编译并运行它就会看到!我不明白。在它之后你做了第一轮值和东西它结束并重新开始,但在第 3 行,跳过名称和时间?:(
  • @user2257336 正如我在回答中所说,这是因为您将 getline 与 >> 混合在一起。这个问题在这里被问了数百次。我无法比已经给出的所有答案更好地回答它。看看我上面发布的链接。
【解决方案2】:

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

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-13
    • 1970-01-01
    • 1970-01-01
    • 2015-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多