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

标签 c xcode

我正在编写一个程序,要求用户添加、编辑和打印员工信息。我似乎无法正确计算每位员工的支付金额和税金。我正在尝试使用将结构作为参数并返回值的函数来完成此操作。它将正确计算输入的第一个员工的已付金额和已付税款,但随后会将部分信息从第一个员工传递给后续员工。我不确定我是否正确传递了 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);
      }
   }
}

最佳答案

每次用户输入 3 或 4 时,您都会创建一个新的“employee”变量。您的 case 语句需要用 break 语句而不是 continue 或 return 语句终止。

“return main()”是对main()的函数调用。这会导致当前员工数据库被推送到堆栈上。插入行 printf("%p\n", &employee);在主 for 循环之前,您将看到每次选择第三个和第四个选项时,都会在不同的内存位置创建一个新变量。

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

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

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

关于c - 如何使用struct param正确调用函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31644390/

相关文章:

ios - 如何在 Xcode 7 中启用_BITCODE?

c - x86 与 x86_64 调用约定

c - 使用指针获取数组的长度

c - 为什么这个枚举不起作用?

xcode - 与 Xcode 分开安装 Git

ios - xcode,在模拟器上启动可以,但无法存档

c - 从字符串中读取 char 类型并将其数值分配给 int 类型结构成员

c - 数组输出错误

c++ - XCode 自动停用断点

ios - 更改 SwiftUI 中 NavigationView 的背景颜色