使用数组时,函数不按预期输出

时间:2016-12-11 21:42:42

标签: c++ arrays

大家下午好,

我遇到问题,正在按照我正在处理的工资单程序的预期输出函数。我添加了一个数组,以便区分员工(员工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 number\count*/
        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 ++很陌生,所以它很可能是我想念或忽略的简单。

感谢您提供任何帮助!

1 个答案:

答案 0 :(得分:0)

当您在main中调用函数displayPayroll()时,可以在for循环中执行此操作,迭代7次调用。然后,在你的函数displayPayroll()中,你有另一个for循环,它遍历7个调用。

删除其中一个for循环。

此外,您的主要在完成后返回一个整数是个好主意。