即使条件为真,我的 if-elseif 语句也不会执行

时间:2021-04-09 07:08:19

标签: c if-statement

即使 if/else-if 语句中的条件为真,程序的控制流也总是转到 else 块。变量salary 未分配给任何必要的值(因为我将值分配给if/else-if 块内的变量。

没有语法错误(至少编译器没有显示)。

#include <stdio.h>
#include <stdlib.h>

int main()
{
  int ID, days = 30, exp, bonus;
  long int salary;
  char nm[30], typ[30];
  printf("Enter the employee name\n");
  //gets(nm);
  scanf("%[^\n]", &nm);
  printf("Enter the employee ID\n");
  scanf("%d", &ID);
  fflush(stdin);
  printf("Enter the type of employee\n");
  scanf("%[^\n]", &typ);

  if (typ == "Permanent" || typ == "P" || typ == "p" || typ == "permanent")
    salary = 1000 * days;
  else if (typ == "Temporary" || typ == "t" || typ == "T" || typ == "temporary")
    salary = 400 * days;
  else
    printf("Invalid type\n");

  printf("Enter the years of experince the employee has\n");
  scanf("%d", &exp);
  bonus = exp * 1000;
  salary += bonus;
  printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
  printf("--------Employee salary slip--------\n");
  printf("`````````````````````````````````````\n");
  printf("Employee Name : %s\n", nm);
  printf("Employee ID : %d\n", ID);
  printf("Type : %s\n", typ);
  printf("Salary : %li", salary);
  return 0;
}

0 个答案:

没有答案