You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

结构体数组员工工资计算与排序问题及代码修正咨询

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_file function only stores raw lines of text in a string array, but doesn’t parse the data into your Record structs—so your calculate_wage function has no valid data to work with.
  • The calculate_wage function is written to accept a single Record reference, but you’re trying to access it like an array. Plus, the atoi usage 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:

  1. Data Parsing Overhaul:

    • The read_file function now takes your Record array directly. Using istringstream, 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.
  2. Fixed Wage Calculation:

    • The calculate_wage function now accepts the full Record array and employee count. It loops through each employee and computes wage as rate * hrWorked—simple, direct, and uses the already parsed numeric values (no need for messy type conversions here).
  3. Added Sorting:

    • We included <algorithm> for std::sort. The lambda function passed to sort defines the comparison rule: we sort by wage in ascending order. To switch to descending order, just change a.wage < b.wage to a.wage > b.wage.
  4. Added Print Function:

    • The print_payroll helper function makes it easy to view the data before and after sorting, so you can verify the results.

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 06:43:22