【发布时间】:2017-04-09 19:47:07
【问题描述】:
美好的一天
我编写了一个代码,可以为员工输出工资单。
此外,尽管进行了大量研究(我试图自己弄清楚),但我不确定如何让我的 for 循环允许我在同一个输出屏幕上连续输入 5 位不同员工的信息。当我运行该程序时,它允许我输入工资单的所有信息,除了每个新工资单开头的员工姓名。
我是一名初学者,希望尽可能多地学习,因此不胜感激。
我的代码如下:
#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;
}
【问题讨论】:
-
混合 getline 和 cin >> int,参见 stackoverflow.com/questions/5739937/… 或缩写形式,尝试将其放在 getData 的末尾: cin.ignore(std::numeric_limits<:streamsize>::max (), '\n');
-
如果我在其他两个下使用 getline 则表示有错误
-
我该如何正确声明呢?
标签: c++