【问题标题】:Salary with vector array to get sum of employee gross pay带向量数组的工资以获得员工总工资的总和
【发布时间】:2019-10-11 19:29:46
【问题描述】:

我要创建一个应用程序,该应用程序至少需要 5 名员工的身份证、姓名、工资率和工作时间。然后我要将工资率和工作时间加在一起,以在初始查询结束时显示每个员工的总工资。我被困在如何将它添加到向量中请帮助!

**这是我们的老师给我们的作业**

http://itweb.fvtc.edu/ag/?u=3&f=cpp-assignment4

我添加了一个向量并添加了员工的所有基本信息

#include <iostream>
#include <conio.h>
#include <string>
#include <vector>

using namespace std;


struct Employee
{
    int id;
    string firstName;
    string lastName;
    float payRate;
    int hours;

};


int main()
{
    /*========= other way of adding employee information ==========*/

    /*const int NUM_EMPLOYEE = 5;
    Employee employee[NUM_EMPLOYEE];



    for (int i = 0; i < 5; i++)
    {
        cout << "ID of employee " << (i + 1) << ": ";
        cin >> employee[i].id;

        cout << "First Name of employee " << (i + 1) << ": ";
        cin >> employee[i].firstName;

        cout << "Last Name of employee " << (i + 1) << ": ";
        cin >> employee[i].lastName;

        cout << "Pay rate for employee " << (i + 1) << ": ";
        cin >> employee[i].payRate;

        cout << "Hours worked " << (i + 1) << ": ";
        cin >> employee[i].hours;
    }*/

    /*========= End of other way of adding employee information ==========*/

    vector<Employee> employees;

    char Another = 'y';

    while (Another == 'y' || Another == 'Y')
    {
        Employee e;
        cout << "Enter employee ID: \n";
        cin >> e.id;
        cout << "Enter employee first name: \n";
        cin >> e.firstName;
        cout << "Enter employee last name: \n";
        cin >> e.lastName;
        cout << "Enter employee pay rate: \n";
        cin >> e.payRate;
        cout << "Enter employee hours worked: \n";
        cin >> e.hours;
        employees.push_back(e);

        cout << "Another? (y/n): \n";
        cin >> Another;

    }

    float sum = 0;

    vector<Employee>::iterator it = employees.begin();

    for (; it != employees.end(); it++)
    {
        cout << "ID of employee:  \n" << it->id << ": \n" 
            << "Employees name: \n" << it->firstName << " " << it->lastName << ": \n" 
            << "Employee pay rate: \n" << it->payRate << ": \n" 
            << "Employee hours worked: \n" << it->hours << "\n";
    }

    float avg = sum / employees.size();

    Employee e;

    /*cout << " ID of employees: \n" << e.id;
    cout << " Name of employees: \n" << e.firstName << " " << 
 e.lastName;*/
    cout << "Gross pay of employees: \n" << avg;







    _getch();
    return 0;

}

向用户显示所有员工的 ID、姓名和总工资

【问题讨论】:

  • 对我来说看起来很合理。如果您不小心在需要整数的地方输入了字符串,可能会出现问题,因此我强烈建议您在使用输入的任何值之前测试流是否成功输入,
  • 您是否特别关注“总工资”部分?您所要做的就是像打印员工信息一样遍历员工,然后计算it-&gt;payRate * it-&gt;hours。然后你可以把它放在Employee 的新字段中,或者打印它,或者你需要做的任何其他事情。
  • 当您打印员工时,您不会对 sum 做任何事情。
  • 见函数std::toupper()。像if (toupper(Answer) == 'Y') 一样使用它。不需要两个比较。如果你不喜欢toupper,请查看它的兄弟tolower
  • @0x5453 好吧,我仍然不知道如何让它工作。你刚刚给我的it-> payrate,由于我运行它时出现一些调试错误,它仍然无法正常工作。这是我更新的代码。

标签: c++ arrays vector


【解决方案1】:
    vector<Employee> employees;

    char Another = 'y';

    while (Another == 'y' || Another == 'Y')
    {
        Employee e;
        cout << "Enter employee ID: \n";
        cin >> e.id;
        cout << "Enter employee first name: \n";
        cin >> e.firstName;
        cout << "Enter employee last name: \n";
        cin >> e.lastName;
        cout << "Enter employee pay rate: \n";
        cin >> e.payRate;
        cout << "Enter employee hours worked: \n";
        cin >> e.hours;
        employees.push_back(e);

        cout << "Another? (y/n): \n";
        cin >> Another;

    }

    float sum = 0;

    vector<Employee>::iterator it = employees.begin();

    cout << "ID" << "\t" << "First Name" << "\t" << "Last Name" << "\t" << "Pay rate" << "\t" << "Hours" << "\t" << "Gross Pay" << "\n" ;

    for (; it != employees.end(); it++)
    {
        float grossPay = it->payRate * it->hours;
        cout << it->id << "\t" << it->firstName << "\t\t" << it->lastName << "\t\t" << it->payRate << "\t\t" 
            << it->hours << "$" << "\t" << grossPay << "\n";
        sum += grossPay;
    }

    cout << "The gross pay for all employee is: " << "$" << sum;


    _getch();
    return 0;

}

//This is what i got so far. But if I want to make the application ask the user to input how many employee they want to enter into the database how do i do that? would it be like this?


int EMPLOYEE_NUM[][]    // ??

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-17
    • 1970-01-01
    • 2016-08-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多