【问题标题】:Employee Pay program in C, with arrays and structsC中的员工薪酬程序,带有数组和结构
【发布时间】:2013-04-24 03:10:54
【问题描述】:

这个程序的输出有一些问题。

逻辑似乎没问题,但每当我编译/运行它时,我都会在输出部分得到一个 (11db)(我使用 Xcode)。

它说“构建成功”并且代码本身没有显示任何错误。

基本上,您必须从输入文件“clock.txt”中读取一些值,该文件包含:

*员工数 (n) *他们的名字和姓氏,然后是他们的工资率,(你把它放在一个名为员工的结构中)*周数 (k)

然后,您必须计算一周的总工资(超过,总税额,以及净工资(总 - 税)。

这是我到目前为止所写的:

#include <stdio.h>
#include <string.h>
#define MAX_LEN 30

struct employee {
    char first[MAX_LEN];
    char last[MAX_LEN];
    double payperhr;
    double gross;
    double taxes;
    double hours_in_week;
} ;

int main() {
    FILE *ifp, *ofp;
    ifp = fopen("clock.txt", "r");
    ofp = fopen("w2.txt", "w");
    int i, j, n, k, m, l, matchLast, found;
    int hourIn, minIn, hourOut, minOut;
    double total_time_day = 0.0;
    double under40tax, over40tax, tax;
    double netPay;

    char  firstRead[MAX_LEN],  lastRead[MAX_LEN], lastName[MAX_LEN], firstName[MAX_LEN];
    float rateRead;

    fscanf(ifp, "%d", &n);


    struct employee arrayEmployees[n];

    for (i=0; i<n; i++) {
        fscanf(ifp, "%s", firstRead);
        fscanf(ifp, "%s", lastRead);
        fscanf(ifp, "%f", &rateRead);

        strcpy(arrayEmployees[n].first, firstRead);
        strcpy(arrayEmployees[n].last, lastRead);
        arrayEmployees[n].payperhr = rateRead;
    }

    fscanf(ifp, "%d", &k);
    fscanf(ifp, "%d", &m);

    for (i=0; i<k; i++) {
        for (j=0; j<m; j++) {
            fscanf(ifp, "%s", lastName);
            fscanf(ifp, "%s", firstName);

            for (l=0; l<n; l++)
                matchLast = strcmp(lastName, arrayEmployees[l].last);
            if (matchLast == 0) {
                found = matchLast;
                strcpy(arrayEmployees[i].last, lastName);
                strcpy(arrayEmployees[i].first, firstName);

                fscanf(ifp, "%d%d%d%d", &hourIn, &minIn, &hourOut, &minOut);
                hourIn%=12;
                hourOut%=12;
                total_time_day = (((hourOut*1.0) - hourIn)+((minOut*1.0) - minIn))/60;
                arrayEmployees[j].hours_in_week += total_time_day;
                arrayEmployees[j].gross += total_time_day*rateRead;
            }
        }
        if (arrayEmployees[i].hours_in_week/40<1) {
            under40tax = arrayEmployees[j].gross*.1;
            tax = under40tax;
            arrayEmployees[i].taxes = tax;
        }
        else if (arrayEmployees[j].hours_in_week/40>1) {
            over40tax = (40*.1)+((arrayEmployees[j].hours_in_week-40)*.2);
            arrayEmployees[i].gross -= 40;
            arrayEmployees[i].gross = arrayEmployees[i].hours_in_week*1.5;
            arrayEmployees[i].gross +=40;
            tax = over40tax;
            arrayEmployees[i].taxes = tax;
        }
        netPay = arrayEmployees[i].gross - tax;
        arrayEmployees[i].hours_in_week = 0.0;

        fprintf(ofp, "W2 Form\n");
        fprintf(ofp, "-----");
        fprintf(ofp, "Name: %s %s", arrayEmployees[i].last, arrayEmployees[i].first);
        fprintf(ofp, "Gross Pay:  %lf", arrayEmployees[i].gross);
        fprintf(ofp, "Taxes Withheld:  %lf", arrayEmployees[i].taxes);
        fprintf(ofp, "Net Pay:  %lf", netPay);

        arrayEmployees[i].hours_in_week = 0.0;
        arrayEmployees[i].taxes = 0.0;
        arrayEmployees[i].gross = 0.0;
    }
    return 0;
}

【问题讨论】:

  • n在编译时不是常量,怎么能structemployee arrayEmployees[n];编译?
  • @tacp 这仍然是合法的C99。它是一个变长数组。
  • 你试过使用调试器吗?
  • 我很确定您在main 的第一个for 循环中指的是arrayEmployees[i] 而不是arrayEmployees[n]。您也永远不会检查 ifpofp 是否为 NULL。
  • @user2044189,就在您为每个人致电fopen 之后!此外,无论何时您是 replying to a comment,请使用“@”符号,立即 后跟名称,再跟一个空格。

标签: c arrays struct


【解决方案1】:

这里有问题

 for (i=0; i<n; i++) {
    fscanf(ifp, "%s", firstRead);
    fscanf(ifp, "%s", lastRead);
    fscanf(ifp, "%f", &rateRead);

    strcpy(arrayEmployees[n].first, firstRead);
    strcpy(arrayEmployees[n].last, lastRead);
    arrayEmployees[n].payperhr = rateRead;
}

    for (j=0; j<m; j++) {
        fscanf(ifp, "%s", lastName);
        fscanf(ifp, "%s", firstName);

        for (l=0; l<n; l++)
            matchLast = strcmp(lastName, arrayEmployees[l].last);
        if (matchLast == 0) {
            found = matchLast;
            strcpy(arrayEmployees[i].last, lastName);
            strcpy(arrayEmployees[i].first, firstName);

            fscanf(ifp, "%d%d%d%d", &hourIn, &minIn, &hourOut, &minOut);
            hourIn%=12;
            hourOut%=12;
            total_time_day = (((hourOut*1.0) - hourIn)+((minOut*1.0) - minIn))/60;
            arrayEmployees[j].hours_in_week += total_time_day;
            arrayEmployees[j].gross += total_time_day*rateRead;
        }
    }

其实应该是

 for (i=0; i<n; i++) {
    fscanf(ifp, "%s", firstRead);
    fscanf(ifp, "%s", lastRead);
    fscanf(ifp, "%f", &rateRead);

    strcpy(arrayEmployees[i].first, firstRead);
    strcpy(arrayEmployees[i].last, lastRead);
    arrayEmployees[i].payperhr = rateRead;
}

    for (j=0; j<m; j++) {

        fscanf(ifp, "%s", lastName);
        fscanf(ifp, "%s", firstName);

        for (l=0; l<n; l++){

            matchLast = strcmp(lastName, arrayEmployees[l].last);

            if (matchLast == 0) {

                found = matchLast;
                strcpy(arrayEmployees[j].last, lastName);
                strcpy(arrayEmployees[j].first, firstName);

                fscanf(ifp, "%d%d%d%d", &hourIn, &minIn, &hourOut, &minOut);

                hourIn%=12;
                hourOut%=12;
                total_time_day = (((hourOut*1.0) - hourIn)+((minOut*1.0) - minIn))/60;

                arrayEmployees[j].hours_in_week += total_time_day;
                arrayEmployees[j].gross += total_time_day*rateRead;
            }
        }
    }

【讨论】:

  • 我修复了这个问题,但它仍然没有编译,所以我插入了一个 if 语句来检查 NULL 并且它通过了。所以文件甚至没有打开。如何修复无法读取的文件?
  • @user2044189 如果没有输入和输出,我无法弄清楚 k 和 m 是什么,但我认为看到代码我觉得我修改后的答案应该就是你所需要的
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-03-31
  • 2011-04-11
  • 1970-01-01
  • 1970-01-01
  • 2013-09-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多