将正确的参数传递给函数

时间:2019-07-09 02:12:02

标签: c

帮我将正确的参数传递给我的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;
}

1 个答案:

答案 0 :(得分:0)

假设成绩是一个字符。 (A,B,C等)

  1. 函数int calculateSalary(char Grade, int hours_worked)声明正确。
  2. 结构声明需要更改

    struct Employee
    {
        String name;
        char grade;
        int hours_worked;
    };    
    
  3. 扫描成绩的方式应为%c。还请在前面添加一个空格,以删除前一个%s之后剩余的行尾字符。另外,printf也需要更改

    scanf(" %c", &arr_employee[i].grade);     
    ..
    printf("Grade: %c \n",arr_employee[i].grade);
    
  4. calculateSalary中,您需要与字符进行比较

    if(Grade=='A')