0

I want to calculate the HRA, DA etc. I have to define a constructor, But the error pops up mentioning that The variable `basic_salary' is assigned but its value is never used.

code:

public void read()
{
    Console.WriteLine("Enter the employee name");
    empname = Console.ReadLine();
    Console.WriteLine("Enter the basic salary of an employee");
    int basic_salary = Convert.ToDouble(Console.ReadLine());
    calculate();
}

class program
{
public static void Main(string[] args)
{
    Employee employeobj = new Employee();
    employeobj.read();
    employeobj.display();
}
}

1 Answers1

1

In read() function you are defining basic_salary variable again, instead of using basic_salary defined at the class level.

To solve your issue, try

public void read()
{
    Console.WriteLine("Enter the employee name");
    empname = Console.ReadLine();
    Console.WriteLine("Enter the basic salary of an employee");
    //insted of defining new integer as basic_salary, use existing class level basic_salary variable
    basic_salary = Convert.ToInt32(Console.ReadLine()); 
    calculate();
}
Prasad Telkikar
  • 15,207
  • 5
  • 21
  • 44
  • but still when I input basic salary as 1000 the hra is coming 0, please help me with that as well –  Apr 05 '21 at 06:53
  • You are getting `0` as a result because of type of `HRA`. Change type of `HRA, DA, GS, incometax, netsalary` to double or decimal. Helpfult like https://stackoverflow.com/questions/10851273/why-does-integer-division-in-c-sharp-return-an-integer-and-not-a-float – Prasad Telkikar Apr 05 '21 at 06:55
  • okay I edited the above code, is it correct ? –  Apr 05 '21 at 07:00
  • 1
    okay it worked after reading the above post by changing 100 to 100.0 & others as well thanks alot –  Apr 05 '21 at 07:04