0

the flow of control of the program always goes to the else block even if the condition inside the if/else-if statement is true. The variable salary does not get assigned to any of the necessary values (because I'm assigning the values to the variable inside the if/else-if block.

There's no syntax errors (at least none shown by the compiler).

#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;
}
Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
Aloy
  • 13
  • 4
  • 3
    to compare strings you need use strcmp and not the == operator – CannedMoose Apr 09 '21 at 07:12
  • There is no computer in common use (and probably not in existence) for which a string is a fundamental `imm` data type. And C, unlike some languages, does not abstract that away or kludge the `==` operator to pretend that they are. You are comparing an object that (probably) exists in `.rodata` with an object on the stack. Those objects are not equivalent, even if the data they hold is the same. – torstenvl Apr 09 '21 at 07:22
  • OT: You should indent your code. Your unidented code makes the if/else statements unreadable. I've done it for you. – Jabberwocky Apr 09 '21 at 07:31

0 Answers0