简体   繁体   English

if语句和for()在C中具有结构的函数中循环

[英]if statements and for() loops in a function with structs in C

I know this is a very novice question but I am having trouble with my if and for() loops in this program. 我知道这是一个非常新手的问题,但是我在程序中的if和for()循环遇到了麻烦。 I tried to designate where the issues are, but basically in main() it asks if the user wants to dismiss an employee and then that employee ID. 我试图指定问题的出处,但基本上是在main()中询问用户是否要解雇一名雇员,然后询问该雇员ID。 Then in the dismissWorker function, it's supposed to scan the array of worker ID's, find the ID that matches the employee to be fired (badID), and then change that array. 然后在dismissWorker函数中,应该扫描工作者ID的数组,找到与要解雇的雇员匹配的ID(badID),然后更改该数组。 In my dismissWorker function I have it printing "HI" just to see if I called it correctly in main (and it does), but it doesn't run through the for() loop. 在我的dismissWorker函数中,我打印了“ HI”,只是看它是否在main中正确调用了它(确实如此),但它没有通过for()循环运行。 I'm sure it's something simple that I'm doing wrong, but what is it exactly? 我确定我做错了很简单,但这到底是什么呢? Also, I don't know what I should have it return to at the end of dismissWorker, so some suggestions would be helpful. 另外,我不知道在dismissWorker结束时应该返回什么内容,因此一些建议会有所帮助。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define MAX_ROSTER 10 //roster of employees

typedef struct workerT_struct {
    char name[81]; //employee name
    char title[81]; //employee title
    int empID; //unique ID for employee
    int empStatus; //
    int year100_salary; //before-tax salary, in cents
    int year100_401k; //annual contribution to retirement, in cents
    double taxRate; //fraction used to determine payroll taxes
    int month100_paycheck; //monthly paycheck, in cents
} workerT;

typedef struct companyT_struct {
    char dbaName[81]; //company name
    int EmpActiveTot; //number of active employees
    int EmpOnLeaveTot; //number of employees on unpaid leave
    int year100_salaryTot; //total annual before-tax salaries, in cents
    int year100_401kTot; 
    double year100_taxTot; //total annual payroll tax, in cents
    int month100_paycheckTot; //total of all monthly paychecks for employees
} companyT;

    void initWorkerArray(workerT list[], int siz);
    void prntWorker(workerT list[], int siz, int indx);
    void payWorker (workerT list[], int siz, int indx);
    int dismissWorker (workerT list[], int siz, int badID);

    void initCo(companyT hubCo[], workerT roll [], int sizRoll);
    void doCoBooks(companyT hubCo[], workerT roll[], int sizRoll);
    void printCoBooks(companyT hubCo[]);

int main()
{
    workerT roster[MAX_ROSTER];
    initWorkerArray(roster, MAX_ROSTER);
    payWorker(roster, MAX_ROSTER, 0);

    companyT myCo[1];
    initCo(myCo, roster, 0);
    doCoBooks(myCo, roster, MAX_ROSTER);
    printCoBooks(myCo);
    printf("Would you like to dismiss an existing employee? (yes/no)\n\n");

    //FIXME FIXME FIXME
    char defaultAnswer[4] = "yes";
    char answer[4];
    int badID = 0;
    scanf(" %s", answer);
    while (strcmp(answer,defaultAnswer) == 0) {
        printf("Please give the employee ID:\n");
        scanf(" %d", &badID);
        printf("The employee ID you chose was %d.\n", badID);
        dismissWorker(roster, MAX_ROSTER, 10); //FIXME

        printf("Do you want to dismiss another employee?\n\n");
        scanf(" %s", answer);
        }
    printf("Goodbye!");
    return 0;
}

void initWorkerArray(workerT list[], int siz) {
    int i = 0;
    for (i = 0; i < 4; ++i) {
        strcpy(list[0].name, "Buggy, Orson");
        strcpy(list[0].title, "Director/President");
        list[0].empID = 1;
        list[0].empStatus = 1;
        list[0].year100_salary = 12015000;
        list[0].year100_401k = 950000;
        list[0].month100_paycheck = 0;
        list[0].taxRate = 0.45;

        strcpy(list[1].name, "Czechs, Imelda");
        strcpy(list[1].title, "Chief Financial Officer");
        list[1].empID = 2;
        list[1].empStatus = 1;
        list[1].year100_salary = 8020000;
        list[1].year100_401k = 960000;
        list[1].month100_paycheck = 0;
        list[1].taxRate = 0.39;

        strcpy(list[2].name, "Hold, Levon");
        strcpy(list[2].title, "Customer Service");
        list[2].empID = 6;
        list[2].empStatus = -1;
        list[2].year100_salary = 8575000;
        list[2].year100_401k = 1133000;
        list[2].month100_paycheck = 0;
        list[2].taxRate = 0.39;

        strcpy(list[3].name, "Andropov, Picov");
        strcpy(list[3].title, "Shipping Coordinator");
        list[3].empID = 7;
        list[3].empStatus = 1;
        list[3].year100_salary = 4450000;
        list[3].year100_401k = 375000;
        list[3].month100_paycheck = 0;
        list[3].taxRate = 0.31;
        }

    for (i = 4; i < siz; ++i) {
        strcpy(list[i].name, "none");
        strcpy(list[i].title, "none");
        list[i].empID = -1;
        list[i].empStatus = -1;
        list[i].year100_salary = 0;
        list[i].year100_401k = 0;
        list[i].month100_paycheck = 0;
        list[i].taxRate = 0.0;
         }
    return;
    }

void prntWorker(workerT list[], int siz, int indx) {
    int i = 0;
    for (i = 0; i < siz; ++i) {
        printf("%s, ", list[i].name);
        printf("%s, ", list[i].title);
        printf("%d, ", list[i].empID);
        printf("%d, ", list[i].empStatus);
        printf("%d, ", list[i].year100_salary);
        printf("%d, ", list[i].year100_401k);
        printf("%lf, ", list[i].taxRate);
        printf("%d ", list[i].month100_paycheck);
        printf("\n\n");
    }
    return;
}
void payWorker(workerT list[], int siz, int indx) {
    int i;
    for (i = 0; i < siz; ++i) {
        list[i].month100_paycheck = (list[i].year100_salary / 12);
        }
    prntWorker(list, MAX_ROSTER,0);
    return;
}
//FIXME FIXME FIXME
int dismissWorker (workerT list[], int siz, int badID) {
    int i;
    printf("HI");
    for (i = 0; i < siz; ++i) { //FIXME 
        if (list[i].empID == badID) {
            printf("HIHIHI");
            list[i].empStatus = -1;
            list[i].month100_paycheck = 0;
            list[i].year100_salary = 0;
    return 1;
    }
        else {
        printf("\nWARNING! empID not found! Cannot dismiss "
               "a non-employee!\n\n");
        return 1;
        }
}
return siz;
}
    void initCo(companyT hubCo[], workerT roll [], int sizRoll) {
        initWorkerArray(roll, sizRoll);
        strcpy(hubCo[0].dbaName, "Dewey, Cheatham, and Howe, Consultants");
        hubCo[0].EmpActiveTot = 3;
        hubCo[0].EmpOnLeaveTot = 0;
        hubCo[0].year100_salaryTot = 0;
        hubCo[0].year100_401kTot = 0;
        hubCo[0].year100_taxTot = 0.0;
        hubCo[0].month100_paycheckTot = 0;
}
void doCoBooks(companyT hubCo[], workerT roll[], int sizRoll) {
    int i = 0;
    for (i = 0; i < sizRoll; ++i) {
            if (roll[i].empStatus == 1) {
                payWorker(roll, MAX_ROSTER, i);
                hubCo[0].year100_salaryTot += roll[i].year100_salary;
                hubCo[0].year100_401kTot += roll[i].year100_401k;
                hubCo[0].year100_taxTot += roll[i].taxRate;
                hubCo[0].month100_paycheckTot += roll[i].month100_paycheck;
            }
    }
}
void printCoBooks(companyT hubCo[]) {
        printf("Name: %s\n", hubCo[0].dbaName);
        printf("Active: %d\n", hubCo[0].EmpActiveTot);
        printf("Leave: %d\n", hubCo[0].EmpOnLeaveTot);
        printf("Salary: %d\n", hubCo[0].year100_salaryTot);
        printf("401k: %d\n", hubCo[0].year100_401kTot);
        printf("Monthly: %d\n", hubCo[0].month100_paycheckTot);
        printf("Tax: %lf\n", hubCo[0].year100_taxTot);
        printf("\n\n");
}

You'll be able to see the error in your logic clearly if you use proper indentation in your code. 如果在代码中使用适当的缩进,您将能够清楚地看到逻辑中的错误。

int dismissWorker (workerT list[], int siz, int badID) {
   int i;
   printf("HI");
   for (i = 0; i < siz; ++i) { 
      if (list[i].empID == badID) {
         printf("HIHIHI");
         list[i].empStatus = -1;
         list[i].month100_paycheck = 0;
         list[i].year100_salary = 0;
         return 1;
      }
      else {
         printf("\nWARNING! empID not found! Cannot dismiss "
                "a non-employee!\n\n");
         return 1;
      }
   }
   return siz;
}

As you can see, if the ID of the first employee doesn't match the given badID , the function will return with the warning. 如您所见,如果第一位员工的ID与给定的badID不匹配,该函数将返回警告。 You need to use: 您需要使用:

// Return 1 for success and 0 for failure, maybe.
int dismissWorker (workerT list[], int siz, int badID) {
   int i;
   printf("HI");
   for (i = 0; i < siz; ++i) { 
      if (list[i].empID == badID) {
         printf("HIHIHI");
         list[i].empStatus = -1;
         list[i].month100_paycheck = 0;
         list[i].year100_salary = 0;
         return 1;
      }
   }

   printf("\nWARNING! empID not found! Cannot dismiss "
          "a non-employee!\n\n");

   return 0;
}

In your function initWorkerArray(workerT list[], int siz) you initiate four workerT objects with empID values 1, 2, 6, 7. 在函数initWorkerArray(workerT list[], int siz) ,启动四个empID值为workerT对象。

Now, in the main function, you call dismissWorker with dismissWorker(roster, MAX_ROSTER, 10) 现在,在main函数中,使用dismissWorker(roster, MAX_ROSTER, 10)调用dismissWorker

In the for loop: 在for循环中:

for (i = 0; i < siz; ++i) { //FIXME 
    if (list[i].empID == badID) {
        printf("HIHIHI");
        list[i].empStatus = -1;
        list[i].month100_paycheck = 0;
        list[i].year100_salary = 0;
    return 1;
    }
}

As far as I can see, the code does run through the for loop, but it can't find the badID and exits. 据我所知,代码确实通过for循环运行,但是找不到badID并退出。 You should print a warning or an error message to catch errors, such as these. 您应该打印警告或错误消息以捕获诸如此类的错误。

Try dismissWorker(roster, MAX_ROSTER, 7) to test it. 尝试使用dismissWorker(roster, MAX_ROSTER, 7)进行测试。 It should work, and use error / warning messages to catch unforeseen circumstances in the code, such as empID 's that don't exist in this case. 它应该可以工作,并使用错误/警告消息来捕获代码中无法预料的情况,例如在这种情况下不存在的empID Commonly known as Defensive Programming 俗称防御性编程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM