I have this program, okay I need the BasicSalary function to take in the parameters as indicated. I have tried to understand the error from my logs, someone help me define this function properly.
#include <stdio.h>
#include <stdlib.h>
struct Employee {
char Em_name[40];
int Em_hours;
char Em_grade[2];
int Em_id;
};
struct Employee Emps[10];
int total_emps;
void newEmployee() {
int i;
for (i = 0; i < total_emps; i++) {
printf(
"\n***********************NEW EMPLOYEE*****************************");
printf("\nEmployee Assigned id :\t\t %d", i + 1);
printf("\nEnter Employee Name :\t\t");
scanf("%s", Emps[i].Em_name);
printf("Enter Employee Grade :\t\t ");
scanf("%s", Emps[i].Em_grade);
printf("Enter Hours Worked :\t\t");
scanf("%d", &Emps[i].Em_hours);
printf("\t \t__________SAVED*************\n");
}
payslips();
}
void payslips() {
printf("*********************************************************\n");
printf("_______________________PAYSLIPS_______________________\n");
printf("*********************************************************\n");
printf("Employee Name\tTotal Hours\tBasic Salary ");
int i;
for (i = 0; i < total_emps; i++) {
char *name = Emps[i].Em_name;
char *grade = Emps[i].Em_grade;
int *hours = Emps[i].Em_hours;
printf("\n%s\t\t", name);
printf("%d\t\t", hours);
int salary = BasicSalary(hours, grade);
printf("%d", salary)
}
printf("\n*********************************************************\n");
printf("_______________________END PAYSLIPS_______________________\n");
printf("*********************************************************\n");
}
int BasicSalary(int hours, char grade) {
if (grade == 'A') {
return hours * 500;
} else if (grade == 'B') {
return hours * 1000;
} else {
return 0;
}
}
int main() {
printf("Enter number of Employees: ");
scanf("%d", &total_emps);
newEmployee();
}
转载于:https://stackoverflow.com/questions/53038068/how-to-pass-pointer-parameters-to-functions
I would simplify the code, you don't need to use as many pointers and they should be avoided as they are a common source of bugs.
Grade is just a single character, store it as such
struct Employee {
char Em_name[40];
int Em_hours;
char Em_grade;
int Em_id;
};
They read it as a single character, like you are the integer.
scanf(" %c", &Emps[i].Em_grade);
Now extract the values, no need to use pointers except for the string.
char *name = Emps[i].Em_name;
char grade = Emps[i].Em_grade;
int hours = Emps[i].Em_hours;
printf("\n%s\t\t", name);
printf("%c\t\t", grade); // Throw in for debugging
printf("%d\t\t", hours);
int salary = BasicSalary(hours, grade);
printf("%d", salary);