1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
#include <iostream> #include <vector> #include <iomanip> class Employee { public: Employee(const std::string& name, const std::vector<double>& performanceScores) : name(name), performanceScores(performanceScores) {} std::string getName() const { return name; } double getAverageScore() const { double sum = 0; for (double score : performanceScores) { sum += score; } return performanceScores.empty() ? 0 : sum / performanceScores.size(); } private: std::string name; std::vector<double> performanceScores; }; int main() { int numEmployees; std::cout << "Enter the number of employees: "; std::cin >> numEmployees; std::vector<Employee> employees; for (int i = 0; i < numEmployees; ++i) { std::string name; int numCriteria; std::cout << "Enter name for employee " << i + 1 << ": "; std::cin.ignore(); // To ignore any newline character left in the buffer std::getline(std::cin, name); std::cout << "Enter the number of performance criteria for " << name << ": "; std::cin >> numCriteria; std::vector<double> performanceScores(numCriteria); for (int j = 0; j < numCriteria; ++j) { std::cout << "Enter score for criterion " << j + 1 << ": "; std::cin >> performanceScores[j]; } employees.emplace_back(name, performanceScores); } std::cout << std::fixed << std::setprecision(2); std::cout << "\nEmployee Performance Report:\n"; std::cout << "Name\tAverage Score\n"; for (const auto& employee : employees) { std::cout << employee.getName() << "\t" << employee.getAverageScore() << "\n"; } return 0; } |
Explanation
- Class
Employee
:- Purpose: Represents an employee and their performance evaluation.
- Attributes:
name
: Name of the employee.performanceScores
: Vector storing performance scores for various criteria.
- Methods:
getName()
: Returns the employee’s name.getAverageScore()
: Calculates and returns the average performance score based on the criteria scores.
- Main Function:
- Setup: Initializes a list of employees.
- Input:
- Prompts the user for the number of employees and details for each employee, including their performance scores for different criteria.
- Report Generation:
- Outputs a performance report showing each employee’s name and their average performance score.
Usage
- Performance Evaluation: Simulates a basic employee performance evaluation system where each employee’s performance is rated on multiple criteria.
- User Interaction: Allows users to input performance data and generate a report of average performance scores.