结构体数组员工工资计算与排序问题及代码修正咨询
Fixing Wage Calculation & Employee Sorting in Your C++ Code
Let’s break down the issues in your current code and fix them, plus add the sorting functionality you need.
First, The Key Problems in Your Current Code:
- Your
read_filefunction only stores raw lines of text in a string array, but doesn’t parse the data into yourRecordstructs—so yourcalculate_wagefunction has no valid data to work with. - The
calculate_wagefunction is written to accept a singleRecordreference, but you’re trying to access it like an array. Plus, theatoiusage here is completely incorrect (you’re using an uninitialized char array, which leads to undefined behavior). - You’re missing the necessary code to sort employees by their calculated wages.
Here’s the Corrected, Complete Code:
#include <iostream> #include <fstream> #include <string> #include <sstream> #include <algorithm> // Required for std::sort using namespace std; struct Record { string name; int id; double rate; int hrWorked; double wage; }; // Modified to parse directly into Record array void read_file(Record payroll[], int employeeCount); // Now takes the full array and count to calculate wages void calculate_wage(Record payroll[], int employeeCount); // Helper function to print payroll data (optional but useful) void print_payroll(Record payroll[], int employeeCount); int main() { const int employeeCount = 5; Record payroll[employeeCount]; cout << "Reading employee data from file:\n"; read_file(payroll, employeeCount); cout << "\nCalculating wages...\n"; calculate_wage(payroll, employeeCount); cout << "\nPayroll before sorting:\n"; print_payroll(payroll, employeeCount); // Sort employees by wage (ascending order; change to > for descending) sort(payroll, payroll + employeeCount, [](const Record& a, const Record& b) { return a.wage < b.wage; }); cout << "\nPayroll sorted by wage (ascending):\n"; print_payroll(payroll, employeeCount); system("pause"); return 0; } void read_file(Record payroll[], int employeeCount) { ifstream myFile("Project 3.dat"); if (!myFile.is_open()) { cout << "No Data Found!" << endl; return; } string line; int index = 0; while (getline(myFile, line) && index < employeeCount) { istringstream iss(line); // Parse each field directly into the Record struct iss >> payroll[index].name >> payroll[index].id >> payroll[index].rate >> payroll[index].hrWorked; cout << line << endl; index++; } myFile.close(); } void calculate_wage(Record payroll[], int employeeCount) { for (int i = 0; i < employeeCount; i++) { // Wage is simply hourly rate multiplied by hours worked payroll[i].wage = payroll[i].rate * payroll[i].hrWorked; } } void print_payroll(Record payroll[], int employeeCount) { cout << "Name\tID\tRate\tHours\tWage\n"; cout << "----------------------------------------\n"; for (int i = 0; i < employeeCount; i++) { cout << payroll[i].name << "\t" << payroll[i].id << "\t" << payroll[i].rate << "\t" << payroll[i].hrWorked << "\t" << payroll[i].wage << "\n"; } }
What Changed & Why:
Data Parsing Overhaul:
- The
read_filefunction now takes yourRecordarray directly. Usingistringstream, we split each line of the file into the individual fields (name, ID, rate, hours worked) and assign them to the corresponding struct members. No more storing raw strings—data is ready for calculation immediately.
- The
Fixed Wage Calculation:
- The
calculate_wagefunction now accepts the fullRecordarray and employee count. It loops through each employee and computeswageasrate * hrWorked—simple, direct, and uses the already parsed numeric values (no need for messy type conversions here).
- The
Added Sorting:
- We included
<algorithm>forstd::sort. The lambda function passed tosortdefines the comparison rule: we sort bywagein ascending order. To switch to descending order, just changea.wage < b.wagetoa.wage > b.wage.
- We included
Added Print Function:
- The
print_payrollhelper function makes it easy to view the data before and after sorting, so you can verify the results.
- The
Testing with Your Sample Data:
For your input file content:
A.Smith 20001 25 40 T.Philip 20002 20 35 S.Long 20003 15 50 G.Santos 20004 30 30 F.Farkas 20005 22 55
The sorted output (ascending wage) will be:
- T.Philip (700) → S.Long (750) → G.Santos (900) → A.Smith (1000) → F.Farkas (1210)
内容的提问来源于stack exchange,提问作者user9206222
相关产品推荐
相关产品推荐

