2013-04-08 81 views
0

我需要为这个℃的答案++的问题,我已经在它的工作,但很明显,我在想念的东西,我会后我的答案迄今太....功能具有不同的返回类型和parameteres C++

写计算并打印工资单。

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

你必须声明三个功能:

1)一个用于输入;

2)一个用于计算员工工资;和

3)一个打印工资单

用户必须输入员工的姓名,工作小时数和小时工资率成变量theEmployeetheHoursWorkedthePayRate。变量employeestring,其他两个变量的类型为float。作为员工的价值观,小时工和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; 
} 
+1

此外,您并没有正确计算加班时间的工资率 - 您应该将问题描述的工资率倍数乘以1.5倍,而不是简单地将其加到工资中。 – riv 2013-04-08 11:40:38

回答

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'被松散地用来表示函数计算的值,然后返回给调用函数,但这并不意味着你的代码中必须有实际的return

顺便说一句,不是你问的问题,但你会遇到麻烦,因为你在混合getline>>。有关说明,请参阅here

+0

函数'printPaySlip'应该返回void。 – sobol6803 2013-04-08 11:43:07

+0

嗨 非常感谢您提供丰富的反馈信息,但是如果您编译并运行它,您将会看到它仍然不会让我输入第二轮名称和内容!我不明白。之后你做了第一轮的值和东西它结束并重新开始,但在第三行,跳过名称和小时?:( – user2257336 2013-04-08 15:20:38

+0

@ user2257336正如我在我的答案中说,这是因为你正在将getline与>>混合。这个问题在这里已经有数百次的问题了,我不能回答它比已经给出的所有答案都好,请看看我上面发布的链接。 – john 2013-04-08 15:36:29

0

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

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

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

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

相关问题