C++ 使用向量数组获取员工工资总额的工资
我要创建一个应用程序,至少需要5名员工的身份证、姓名、工资率和工作时间。然后我要把工资率和工时加在一起,在最初的调查结束时显示每个员工的总工资。我被困在如何添加到矢量请帮助 **这是老师给我们的作业** 我添加了一个向量,并添加了员工的所有基本信息C++ 使用向量数组获取员工工资总额的工资,c++,arrays,vector,C++,Arrays,Vector,我要创建一个应用程序,至少需要5名员工的身份证、姓名、工资率和工作时间。然后我要把工资率和工时加在一起,在最初的调查结束时显示每个员工的总工资。我被困在如何添加到矢量请帮助 **这是老师给我们的作业** 我添加了一个向量,并添加了员工的所有基本信息 #include <iostream> #include <conio.h> #include <string> #include <vector> using namespace std; st
#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->小时数。然后,您可以将其放在Employee的新字段中,或者打印它,或者您需要执行的任何其他操作。当您打印Employee时,您不需要对sum执行任何操作。请参阅函数std::toupper。使用它就像toupperAnswer=='Y'一样。不需要两次比较。如果您不喜欢toupper,请查看它的同级线tolower。@0x5453好的,我仍然不知道如何使它工作。您刚刚给我的it->payrate,由于我运行它时出现了一些调试错误,它仍然无法工作。这是我的更新代码。
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[][] // ??