如何使用struct param正确调用函数?

时间:2015-07-27 03:10:02

标签: c xcode

我正在编写一个程序,要求用户添加,编辑和打印员工信息。我似乎无法正确计算每位员工的已付金额和税金。我试图使用将struct作为参数并返回值的函数来完成它。它将正确计算已支付的金额和为输入的第一个员工支付的税金,但随后它会将一些信息从第一个员工传递给下一个员工。我不确定我是否正确地正确传递了calcpay和calctax函数。

#include <stdio.h>
#include <stdlib.h>

#define SIZE 5

struct database{
   char name[SIZE][20];
   float hours[SIZE];
   float rate[SIZE];
};

void loademployee(struct database* employee){

   for(int i=0; i<SIZE; i++){
      printf("Enter name: ");
      scanf("%s", employee -> name[i]);

      printf("\nEnter hours worked: ");
      scanf("%f", &employee -> hours[i]);

      printf("\nEnter hourly rate: ");
      scanf("%f", &employee -> rate[i]);
   }

   puts("\n");
}

float calcpay(struct database employee){

   if(*employee.hours <= 40){
      return *employee.hours * *employee.rate;
   }
   else{
      return (40 * *employee.rate)+((*employee.hours - 40)*1.5 * *employee.rate);
   }

}

float calctax(struct database employee){

   if(*employee.hours <= 40){
      return *employee.hours * *employee.rate * 0.2;
   }
   else{
      return ((40 * *employee.rate)+((*employee.hours - 40)*1.5 * *employee.rate)) * 0.2;
   }

}

void printemployee(struct database *employee){

   for(int b=0; b<SIZE; b++){
      printf("Pay to: %s\n", employee->name[b]);

      printf("Hours worked: %.2f\n", employee->hours[b]);

      printf("Hourly rate: %.2f\n", employee->rate[b]);

      printf("Amount paid: %.2f\n", calcpay(employee[b]));

      printf("Taxes paid: %.2f\n", calctax(employee[b]));

      printf("Net pay: %.2f\n", calcpay(employee[b]) - calctax(employee[b]));
   }
}


int main(void){

   struct database employee;

   int input, userchoice;

   for(int x=0; x<50; x++){

      printf("1: Add Employee Data ");
      printf("\n2: Update Employee Data ");
      printf("\n3: Print Single Employee ");
      printf("\n4: Print All Employees");
      printf("\n5: Exit ");
      printf("\nSelect an Option: ");
      scanf("%d", &input);

      switch(input){
         case 1:

            loademployee(&employee);
            continue;

         case 2:

            printf("Select Employee to Update: \n");
            for(int r=0; r<SIZE; r++){

               printf("%d.%s\n", r, employee.name[r]);
            }
            scanf("%d", &userchoice);

            for(int y=0; y<SIZE; y++){

               if(y==userchoice){

                  printf("Enter name: ");
                  scanf("%s", employee.name[y]);

                  printf("\nEnter hours worked: ");
                  scanf("%f", &employee.hours[y]);

                  printf("\nEnter hourly rate: ");
                  scanf("%f", &employee.rate[y]);
               }
               continue;
            }

         case 3:

            for(int s=0; s<SIZE; s++){
               for(int j=0; j<SIZE; j++){
                  printf("%d.%s\n", j, employee.name[j]);
               }

               printf("Select an Employee to print: ");
               scanf("%d", &userchoice);

               for(int z=0; z<SIZE; z++){

                  if(userchoice == z){

                     printf("Pay to: %s\n", employee.name[z]);

                     printf("Hours worked: %f\n", employee.hours[z]);

                     printf("Hourly rate: %f\n", employee.rate[z]);

                     printf("Amount paid: %.2f\n", calcpay(employee));

                     printf("Taxes paid: %.2f\n", calctax(employee));

                     printf("Net pay: %.2f\n", calcpay(employee)-        calctax(employee));

                     return main();

                  }
               }
            }


         case 4:

            printemployee(&employee);

            return main();

         case 5:

            exit(1);
      }
   }
}

1 个答案:

答案 0 :(得分:0)

您正在创建一个新的&#34;员工&#34;每次用户输入3或4时都会变量。您的case语句需要使用break语句而不是continue语句或return语句来终止。

&#34;返回main()&#34;是对main()的函数调用。这会导致当前员工数据库被压入堆栈。插入行printf(&#34;%p \ n&#34;,&amp; employee);在主for循环之前,每次选择第3和第4个选项时,您将看到在不同的内存位置创建了一个新变量。

您应该使用break语句,而不是continue语句和返回语句。

此外,您需要初始化员工数据库以确保安全:

struct database{
char name[SIZE][20] = "";
float hours[SIZE] = {0};
float rate[SIZE] = {0};
};