工资与矢量数组,以获得员工总工资的总和

Salary with vector array to get sum of employee gross pay

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

我要创建一个至少需要 5 名员工 ID、姓名、工资率和小时数的应用程序。然后,我将工资率和小时数相加,以显示初始查询结束时每位员工的总工资。我被困在如何将它添加到矢量中,请帮忙!

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

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、姓名和总工资

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 << "tt" << it->lastName << "tt" << it->payRate << "tt" 
<< 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[][]    // ??