使用数组时未预期的功能
function not outputting as expected when utilizing array
大家下午好,
我正在遇到问题,以根据我正在处理的薪资计划的预期输出功能。我添加了一个数组,以便可以区分员工(员工1、2、3等)。这是针对提示用户为每个员工输入小时的工资计划(共有7个)。然后将这些小时分别计算以将小时乘以薪资率,以显示每个员工的总工资。到目前为止,这是我的代码:
PayRoll.h
#include <iostream>
using namespace std;
class Payroll
{
public:
Payroll();
void setHourlyPayRate(double), setHoursWorked(double), setTotalWeeklyPay(double);
double getHourlyPayRate();
double getHoursWorked();
double getTotalWeeklyPay();
private:
double hourlyPayRate, hoursWorked, totalWeeklyPay;
};
Payroll::Payroll()
{
hourlyPayRate = 0.0;
hoursWorked = 0.0;
totalWeeklyPay = 0.0;
}
void Payroll::setHourlyPayRate(double hpr)
{
hourlyPayRate = hpr;
}
double Payroll::getHourlyPayRate()
{
return hourlyPayRate;
}
void Payroll::setHoursWorked(double hw)
{
hoursWorked = hw;
}
double Payroll::getHoursWorked()
{
return hoursWorked;
}
void Payroll::setTotalWeeklyPay(double twp)
{
totalWeeklyPay = twp;
}
double Payroll::getTotalWeeklyPay()
{
return totalWeeklyPay;
}
payroll.cpp
/*Include Section*/
#include <iostream>
#include <iomanip>
#include <string>
#include "Payroll.h"
/*Namespace Section*/
using namespace std;
void displayPayroll(Payroll * const, int);
/*Main section: this is the entry point of the program, which controls the flow of execution*/
int main()
{
const int employee = 7; /*array of employees with maximum of 7*/
int employeeNumber[employee] = { 1,2,3,4,5,6,7 };
Payroll payCycle[employee];
/*Beginning of program. Outputs the following to the user, prompting them to enter the number of hours worked for each of the 7 employees*/
cout << "*******************************************************************************" << endl
<< setw(48) << "Monthly Payroll" << endl
<< "*******************************************************************************" << endl;
for (int index = 0; index < employee; ++index)
{
double hpr, hw, twp; /*initializes hourly rate, hours worked, and twp mutators, accepting decimal values*/
hpr = 7.75; /*Sets hourly pay rate at $7.75*/
char decision = 'Y';
cout << "Enter the number of hours worked for employee " << (index + 1) << ": "; /*looks to array to determine employee numbercount*/
cin >> hw; /*user enters hours worked*/
while (hw >= 61) /*prompts the following outputs to the user if hours worked is greater than maximum value of 60*/
{
cout << "ERROR: Hours must be between 0 and 60" << endl
<< "Are the hours you entered correct (Y or N)? ";
cin >> decision;
if (decision == 'Y' || decision == 'y')
{
cout << endl << "Hours above 60 are considered overtime. Please contact management." << endl << endl;
exit(0);
}
else if (decision == 'N' || decision == 'n')
{
cout << "Please re-enter the total number of hours worked: ";
cin >> hw;
}
}
twp = hpr * hw; /*calculates employee's gross pay for the week by multiplying hourly pay rate by hours worked*/
payCycle[index].setHourlyPayRate(hpr);
payCycle[index].setHoursWorked(hw);
payCycle[index].setTotalWeeklyPay(twp);
}
/*Outputs employee gross pay totals to user, utilizing the twp calculations above*/
cout << endl
<< "*******************************************************************************" << endl
<< setw(52) << "Employee Gross Pay Totals" << endl
<< "*******************************************************************************" << endl;
for (int index = 0; index < employee; ++index)
{
displayPayroll(Payroll * const, int); /* calls to displayPayroll function in order to display gross pay for each of the 7 employees*/
}
system("PAUSE");
}
void displayPayroll(Payroll * const e, int index)
{
/*const int employee = 7;
int employeeNumber[employee] = { 1,2,3,4,5,6,7 };
Payroll payCycle[employee];
for (int index = 0; index < employee; ++index)
cout << "Employee " << (index + 1) << "'s gross pay for this period: $" << e->getTotalWeeklyPay() << endl;*/
//cout << "Employee's gross pay for this period: $" << e->getTotalWeeklyPay() << endl;
cout << "Employee " << (index + 1) << "'s gross pay for this period: $" << e->getTotalWeeklyPay() << endl;
}
输出
*******************************************************************************
Monthly Payroll
*******************************************************************************
Enter the number of hours worked for employee1: 1
Enter the number of hours worked for employee2: 2
Enter the number of hours worked for employee3: 3
Enter the number of hours worked for employee4: 4
Enter the number of hours worked for employee5: 5
Enter the number of hours worked for employee6: 6
Enter the number of hours worked for employee7: 7
*******************************************************************************
Employee Gross Pay Totals
*******************************************************************************
Employee 1's gross pay for this period: $7.75
Employee 2's gross pay for this period: $7.75
Employee 3's gross pay for this period: $7.75
Employee 4's gross pay for this period: $7.75
Employee 5's gross pay for this period: $7.75
Employee 6's gross pay for this period: $7.75
Employee 7's gross pay for this period: $7.75
Employee 1's gross pay for this period: $15.5
Employee 2's gross pay for this period: $15.5
Employee 3's gross pay for this period: $15.5
Employee 4's gross pay for this period: $15.5
Employee 5's gross pay for this period: $15.5
Employee 6's gross pay for this period: $15.5
Employee 7's gross pay for this period: $15.5
Employee 1's gross pay for this period: $23.25
Employee 2's gross pay for this period: $23.25
Employee 3's gross pay for this period: $23.25
Employee 4's gross pay for this period: $23.25
Employee 5's gross pay for this period: $23.25
Employee 6's gross pay for this period: $23.25
Employee 7's gross pay for this period: $23.25
Employee 1's gross pay for this period: $31
Employee 2's gross pay for this period: $31
Employee 3's gross pay for this period: $31
Employee 4's gross pay for this period: $31
Employee 5's gross pay for this period: $31
Employee 6's gross pay for this period: $31
Employee 7's gross pay for this period: $31
Employee 1's gross pay for this period: $38.75
Employee 2's gross pay for this period: $38.75
Employee 3's gross pay for this period: $38.75
Employee 4's gross pay for this period: $38.75
Employee 5's gross pay for this period: $38.75
Employee 6's gross pay for this period: $38.75
Employee 7's gross pay for this period: $38.75
Employee 1's gross pay for this period: $46.5
Employee 2's gross pay for this period: $46.5
Employee 3's gross pay for this period: $46.5
Employee 4's gross pay for this period: $46.5
Employee 5's gross pay for this period: $46.5
Employee 6's gross pay for this period: $46.5
Employee 7's gross pay for this period: $46.5
Employee 1's gross pay for this period: $54.25
Employee 2's gross pay for this period: $54.25
Employee 3's gross pay for this period: $54.25
Employee 4's gross pay for this period: $54.25
Employee 5's gross pay for this period: $54.25
Employee 6's gross pay for this period: $54.25
Employee 7's gross pay for this period: $54.25
Press any key to continue . . .
我已经看了几个小时了,无法对导致这一目标的原因或尾巴造成头或尾巴。我是C 的新手,所以我缺少或忽略了简单的事情。
感谢您提供的任何帮助!
当您在主机中调用函数displayPayroll()
时,您会以7个呼叫进行迭代的循环。然后,在您的函数displayPayroll()
中,您还有另一个通过7个调用迭代的循环。
删除一个for循环。
另外,完成后返回整数是一个好主意。
相关文章:
- 如何实现删除数组的功能?
- 借助功能将 2D 转换为 1D 数组
- 将 2D 数组传递给大小为变量的功能
- 为什么必须动态分配扩展数组才能使此功能正常工作C++
- C++中用于结构的纯数组的类似于TableView/DataFrame的通用功能
- 共享_ptr中多功能数组的类型是什么
- 编写一个递归功能,该功能采用数组并以相反顺序显示元素,而无需在末尾启动数组的索引
- 如何正确填充并将动态数组发送到Delphi中C 中的功能
- 返回处理数组的INT功能
- exc_bad_access on for loop(数组功能指针)
- 如何将浮子数组阵列从Swift传递到C 功能
- 如何将搜索功能添加到数组中
- 可用于任何大小的数组的功能
- 在堆中创建功能指针的数组
- 使用指针在功能中的数组中添加/删除元素
- 创建一个C 模板功能,该功能将返回特定大小的std ::数组
- 基类的通用成员功能数组
- 重复的名称检查功能数组
- 嵌套在功能数组中的循环
- C 从功能数组中调用函数