使用数组时未预期的功能

function not outputting as expected when utilizing array

本文关键字:功能 数组      更新时间:2023-10-16

大家下午好,

我正在遇到问题,以根据我正在处理的薪资计划的预期输出功能。我添加了一个数组,以便可以区分员工(员工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循环。

另外,完成后返回整数是一个好主意。