帮我将正确的参数传递给我的calculateSalary()
#include<stdio.h>
#include<string.h>
typedef char String[256];
int calculateSalary(char Grade, int hours_worked);
struct Employee
{
String name;
char grade[1];
int hours_worked;
};
int main()
{
printf("Welcome to XYZ payroll\n");
Payroll();
return 0;
}
void Payroll(){
int numEmployees,i;
printf("Enter total number of Employees: ");
scanf("%d",&numEmployees);
struct Employee arr_employee[numEmployees];
for(i = 0; i < numEmployees; i++ )
{
printf("\nEnter details of Employee %d\n", i+1);
printf("Enter name: ");
scanf("%s", arr_employee[i].name);
printf("Enter Grade: ");
scanf("%s", &arr_employee[i].grade);
printf("Enter number of hours worked: ");
scanf("%d", &arr_employee[i].hours_worked);
}
printf("\nPAYSLIPS\n\n");
for(i = 0; i < numEmployees; i++ )
{
printf("______________________________\n");
printf("Payslip for employee %d\n",i+1);
printf("Name: %s \n",arr_employee[i].name);
printf("Grade: %s \n",arr_employee[i].grade);
printf("Hours Worked: %d \n",arr_employee[i].hours_worked);
Function Call here
int salary=calculateSalary(arr_employee[i].grade,arr_employee[i].hours_worked);
printf("SALARY: %d \n",salary);
}
}
int calculateSalary(char Grade, int hours_worked){
int salary;
if(Grade=="A"){
salary=500*hours_worked;
}else if(Grade=="B"){
salary=1000*hours_worked;
}else if(Grade=="C"){
salary=1500*hours_worked;
}
return salary;
}
答案 0 :(得分:0)
假设成绩是一个字符。 (A,B,C等)
int calculateSalary(char Grade, int hours_worked)
声明正确。结构声明需要更改
struct Employee
{
String name;
char grade;
int hours_worked;
};
扫描成绩的方式应为%c
。还请在前面添加一个空格,以删除前一个%s
之后剩余的行尾字符。另外,printf
也需要更改
scanf(" %c", &arr_employee[i].grade);
..
printf("Grade: %c \n",arr_employee[i].grade);
在calculateSalary
中,您需要与字符进行比较
if(Grade=='A')