工资核算数组打印输出

时间:2018-03-15 20:25:14

标签: c++

我已尽力清除此计划中的错误,但我仍然没有提出任何问题。我已搜索但尚未找到解决方案。

  

编写一个使用以下数组的程序:

     

•empId:一个包含7个长整数的数组,用于存放员工识别号码。应使用以下数字初始化数组:

     

5658845 4520125 7895122 8777541 8451277 1302850 7580489

     

•小时:七个整数的数组,用于保存每位员工的工作小时数

     

•payRate:每个员工每小时工资率的七个双打数组

     

•工资:保持每位员工工资总额的七个双打

     

程序应该通过下标关联每个数组中的数据。例如,hours数组的元素0中的数字应该是员工的工作小时数,该员工的标识号存储在empId数组的元素0中。同一员工的工资率应存储在payRate数组的元素0中。

     

程序应显示每个员工编号,并要求用户输入该员工的工时和工资率。然后应该计算该雇员的工资总额(小时工资率)并将其存储在工资数组中。

     

为所有员工输入数据后,程序应显示每位员工的识别号码和工资总额。

     

输入验证:不接受小时数的负值或小于15.00的工资率。

我的代码如下:

#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
using namespace std;


int main()
{
    //constant 
    const int empId = 7;
    //arrays initialized with manual entries
    int workers[empId] = { 5658845, 4520125, 7895122,
        8777541, 8451277, 1302850,
        7580489 };
    int hours[empId];
    double payRate[empId];

    //outputs for user entries
    cout << "Please enter the hours worked by " << empId
        << " employees and their\n"
        << "hourly pay rates.\n";
    //loops for each employee
    for (int index = 0; index < empId; index++)
    {
        cout << "Please enter the hours worked by employee number " << (index + 1) << " (ID = " << workers[index] << ") : ";
        cin >> hours[index];
        cout << "Please enter the pay rate for employee number " << (index + 1) << " (ID = " << workers[index] << ") : ";
        cin >> payRate[index];
    }
    do
    {
        cout << "Please enter the hours worked by employee number " << (index + 1) << " (ID = " << workers[index] << ") : ";
        cin >> hours[index];
    }
    if (hours[index] < 0); //data validation
    {
        cout << "Enter in a positive number" << endl;
    }
    while
    {
        (hours[index] < 0)

    do

        cout << "Please enter the pay rate for employee number " << (index + 1) << " (ID = " << workers[index] << ") : ";
        cin >> payRate[index];
    }
    if (payRate[index] < 15)  //data validation for pay rate
    {
        cout << "The pay rate must be >= 15" << endl;
    }
    //outputs for reults
    while (hours[index] < 6);
    {
        cout << "This is the gross pay for each employee:\n";
        cout << fixed << showpoint << setprecision(2);
    }
    for (int index = 0; index < empId; index++)
    {
        double grossPay = hours[index] * payRate[index];
        cout << "Employee #" << (index + 1);
        cout << ": earned $" << grossPay << endl << endl;
    }
    return 0;
}

1 个答案:

答案 0 :(得分:2)

do循环中存在语法错误。

并且您没有遵循您的分配要求:

  • 您的workers数组需要命名为empId

  • 您缺少wages数组

  • 您没有正确验证用户的输入。

尝试更像这样的东西:

#include <iostream>
#include <iomanip>
#include <string>
#include <limits>
#include <cmath>
using namespace std;

int main()
{
    //constant 
    const int numEmployees = 7;

    //arrays initialized with manual entries
    int empId[numEmployees] = { 5658845, 4520125, 7895122, 8777541, 8451277, 1302850, 7580489 };
    int hours[numEmployees];
    double payRate[numEmployees];
    double wages[numEmployees];

    //inputs for user entries
    cout << "Please enter the hours worked by " << numEmployees << " employees\n"
         << "and their hourly pay rates.\n";

    //loops for each employee
    for (int index = 0; index < numEmployees; index++)
    {
        do
        {
            cout << "Please enter the hours worked by Employee # " << (index + 1) << " (ID = " << empId[index] << ") : ";
            if (cin >> hours[index])
            {
                if (hours[index] > 0) //data validation
                    break;

                cout << "Enter in a positive number" << endl;
            }
            else
            {
                cin.ignore(numeric_limits<streamsize>::max(), '\n');
                cin.clear();
                cout << "Enter in a valid number" << endl;
            }
        }
        while (true);

        do
        {
            cout << "Please enter the pay rate for Employee # " << (index + 1) << " (ID = " << empId[index] << ") : ";
            if (cin >> payRate[index])
            {
                if (payRate[index] >= 15.0)
                    break;

                cout << "The pay rate must be >= 15" << endl;
            }
            else
            {
                cin.ignore(numeric_limits<streamsize>::max(), '\n');
                cin.clear();
                cout << "Enter in a valid monetary value" << endl;
            }
        }
        while (true);

        wages[index] = hours[index] * payRate[index];
    }

    //outputs for results

    cout << "This is the gross pay for each employee:\n";
    cout << fixed << showpoint << setprecision(2);

    for (int index = 0; index < numEmployees; index++)
    {
        cout << "Employee #" << (index + 1) << " (ID = " << empId[index] << ") : ";
        cout << "earned $" << wages[index] << endl << endl;
    }

    return 0;
}