如何使用struct param正确调用函数?
我正在编写一个程序,要求用户添加、编辑和打印员工信息。我似乎无法正确计算每位员工的工资和税款。我正试图使用将struct作为参数并返回值的函数来实现这一点。它将正确地计算输入的第一个员工的支付金额和支付的税款,但随后它会将第一个员工的部分信息传递给以下员工。我不确定是否正确地传递了calcpay和calctax函数如何使用struct param正确调用函数?,c,xcode,C,Xcode,我正在编写一个程序,要求用户添加、编辑和打印员工信息。我似乎无法正确计算每位员工的工资和税款。我正试图使用将struct作为参数并返回值的函数来实现这一点。它将正确地计算输入的第一个员工的支付金额和支付的税款,但随后它会将第一个员工的部分信息传递给以下员工。我不确定是否正确地传递了calcpay和calctax函数 #include <stdio.h> #include <stdlib.h> #define SIZE 5 struct database{ cha
#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);
}
}
}
#包括
#包括
#定义尺寸5
结构数据库{
字符名称[大小][20];
浮动小时[大小];
浮动利率[大小];
};
void loademployee(结构数据库*employee){
对于(int i=0;i name[i]);
printf(“\n工时:”);
scanf(“%f”,&employee->hours[i]);
printf(“\n输入小时费率:”);
scanf(“%f”,&employee->rate[i]);
}
卖出(“\n”);
}
float calcpay(结构数据库雇员){
如果(*员工工时率[b]);
printf(“支付金额:%.2f\n”,calcpay(员工[b]);
printf(“已缴税款:%.2f\n”,calctax(员工[b]);
printf(“净薪酬:%.2f\n”,calcpay(员工[b])-calctax(员工[b]);
}
}
内部主(空){
结构数据库;
int输入,用户选择;
对于(int x=0;x每当用户输入3或4时,您都在创建一个新的“employee”变量。您的case语句需要使用break语句而不是continue或return语句终止
“return main()”是对main()的函数调用。这会导致将当前员工数据库推送到堆栈上。在main for循环之前插入printf(“%p\n”,&employee);行,每次选择第3和第4个选项时,都会在不同的内存位置创建一个新变量
应该使用break语句,而不是continue和return语句
此外,您需要初始化员工数据库以确保安全:
struct database{
char name[SIZE][20] = "";
float hours[SIZE] = {0};
float rate[SIZE] = {0};
};