工资与矢量数组,以获得员工总工资的总和
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[][] // ??
相关文章:
- Mongodb c++驱动程序:如何查询元素的数组
- 将数组的地址分配给变量并删除
- 从C++本机插件更新Vector3数组
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 数组索引的值没有增加
- 将对象数组的引用传递给函数
- 为char数组调整zlib-zpipe
- 2D数组来自文本输入,中间有空格
- std::向量与传递值的动态数组
- 在c++中用vector填充一个简单的动态数组
- 使用strcpy将char数组的元素复制到另一个数组
- 使用指针从C++中的数组中获取最大值
- C++使用整数的压缩数组初始化对象
- 告诉一个 const char 数组,除了编译时 C 样式的字符串外,它不以 '