C++ 我需要对计算的输出和在for循环中使用get.line进行5次连续重复的第二个意见
你好 我编写了一个代码,为员工输出工资单 此外,尽管我做了大量研究(我自己也试图弄清楚),但我不确定如何让我的for循环允许我在同一输出屏幕上连续输入5名不同员工的信息。当我运行该程序时,它允许我输入工资单的所有信息,除了每个新工资单开头的员工姓名 我是一个初学者,希望尽可能多地学习,所以任何解释都将不胜感激 我的代码如下:C++ 我需要对计算的输出和在for循环中使用get.line进行5次连续重复的第二个意见,c++,C++,你好 我编写了一个代码,为员工输出工资单 此外,尽管我做了大量研究(我自己也试图弄清楚),但我不确定如何让我的for循环允许我在同一输出屏幕上连续输入5名不同员工的信息。当我运行该程序时,它允许我输入工资单的所有信息,除了每个新工资单开头的员工姓名 我是一个初学者,希望尽可能多地学习,所以任何解释都将不胜感激 我的代码如下: #include <iostream> #include <string> using namespace std; void getDat
#include <iostream>
#include <string>
using namespace std;
void getData (string & theEmployee , float & theHoursWorked, float &
thePayRate)
{
cout<< "Enter the employees name and surname: "<< endl;
getline(cin, theEmployee);
cout << "Enter the numbers of hours the employee worked: " << endl;
cin >> theHoursWorked;
cout << "Enter the employees hourly pay rate?" << endl;
cin >> thePayRate;
}
float calculatePay(const string & theEmployee, float theHoursWorked, float
thePayRate)
{
float regularPay, thePay, overtimeHours;
if (theHoursWorked > 40)
{
regularPay = 40 * thePayRate;
overtimeHours = theHoursWorked - 40;
thePay = regularPay + (overtimeHours * 1.5 * thePayRate);
return thePay
}
else
thePay = theHoursWorked * thePayRate;
return thePay;
}
void printPaySlip(const 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;
}
#包括
#包括
使用名称空间std;
void getData(字符串和员工、float和工时、float和
(工资率)
{
cout至于加班,正如评论中指出的那样,你没有乘以工资率
if (theHoursWorked > 40)
{
regularPay = 40 * thePayRate;
overtimeHours = theHoursWorked - 40;
thePay = regularPay + (overtimeHours * 1.5 * regularPay);
return thePay
} else {
thePay = theHoursWorked * thePayRate;
}
return thePay;
}
至于输入姓名,如果您可以为以后的员工输入工资率等,那么您可能希望对所有3名员工使用相同的代码
cin >> theEmployee;
至于加班费,正如评论中指出的,你错过了乘以工资率
if (theHoursWorked > 40)
{
regularPay = 40 * thePayRate;
overtimeHours = theHoursWorked - 40;
thePay = regularPay + (overtimeHours * 1.5 * regularPay);
return thePay
} else {
thePay = theHoursWorked * thePayRate;
}
return thePay;
}
至于输入姓名,如果您可以为以后的员工输入工资率等,那么您可能希望对所有3名员工使用相同的代码
cin >> theEmployee;
不确定为什么在某些行附近有这些“*”,但以下是您应该更改的内容:
按常量引用传递字符串,而不是按如下()所示的值传递字符串:
去掉奇怪的星号,在一些浮点变量的末尾添加“.f”(现在挑剔),函数calculatePay()不需要“theEmployee”字符串,因为您不在函数体中使用它。不确定为什么在某些行附近有这些“*”,但以下是您应该更改的内容:
按常量引用传递字符串,而不是按如下()所示的值传递字符串:
去掉奇怪的星号,在一些浮点变量的末尾加上“.f”(现在挑剔),函数calculatePay()不需要“theEmployee”字符串,因为您不在函数体中使用它。我永远不会按照您的规则加班。您给的加班费是1.50美元/小时,而不是正常工资的1.5倍。谢谢,我知道我的错误在哪里了。我不想按照我的规则工作。有什么想法吗?我永远不会按照您的规则加班。您给的是1美元加班费为0.50/小时,而不是正常工资率的1.5倍。谢谢,我知道我的错误在哪里了。我不想按照我的规则工作。有什么想法吗?谢谢。我已经编辑了我的代码,但它仍然不允许我输入员工姓名。可能在“getline(cin,theEmployee);”行之前添加“cin.get();”?然后它会阻止我同时运行输出。谢谢。我已经编辑了代码,但是它仍然不允许我输入员工姓名。可能会在“getline(cin,theEmployee);”行之前添加“cin.get();”?然后它会阻止我一起运行输出