Home > database >  IF/ELSE IF statements not doing what I expected them to do. Any thoughts? (C#) [closed]
IF/ELSE IF statements not doing what I expected them to do. Any thoughts? (C#) [closed]

Time:10-07

One of my recent assignments asked the following: Write a program to accept an employee's name and annual salary. Use the following tax rates to calculate and deduct the tax. Output the net salary. (Tax rates: £0 - £10000 = 0%, £10001 - £20000 = 20%, £20001 - £40000 = 25%, £40001 < = 40%) This is what I have come up with:

        string name;
        double annualSalary, netSalary, taxPaid;
        int taxRate;

        Console.Write("Enter the employee's name: ");
        name = Console.ReadLine();

        Console.Write("Enter their annual salary: ");
        annualSalary = Convert.ToDouble(Console.ReadLine());

        if (annualSalary > 0 && annualSalary < 10001)
        {
            taxPaid = 0;
            netSalary = annualSalary - taxPaid;

            Console.WriteLine(name   "\nYour gross salary is £"   annualSalary   "\nYou have paid £"   taxPaid   " in taxes \nYour net salary is £"   netSalary);
        }

        else if (annualSalary > 10000 && annualSalary < 20001)
        {
            taxRate = 20;
            taxPaid = annualSalary * (taxRate / 100);
            netSalary = annualSalary - taxPaid;

            Console.WriteLine(name   "\nYour gross salary is £"   annualSalary   "\nYou have paid £"   taxPaid   " in taxes \nYour net salary is £"   netSalary);
        }
        else if (annualSalary > 20000 && annualSalary < 40001)
        {
            taxRate = 25;
            taxPaid = annualSalary * (taxRate / 100);
            netSalary = annualSalary - taxPaid;

            Console.WriteLine(name   "\nYour gross salary is £"   annualSalary   "\nYou have paid £"   taxPaid   " in taxes \nYour net salary is £"   netSalary);
        }
        else if (annualSalary > 40000)
        {
            taxRate = 40;
            taxPaid = annualSalary * (taxRate / 100);
            netSalary = annualSalary - taxPaid;

            Console.WriteLine(name   "\nYour gross salary is £"   annualSalary   "\nYou have paid £"   taxPaid   " in taxes \nYour net salary is £"   netSalary);
        }
        else
        {
            Console.WriteLine("Invalid input. Try Again.");
        }

However, when I run the program, despite everything looking as it should be, the calculations themselves are wrong. I have quite clearly made an error somewhere that I can't see, but I would be grateful if someone could point me in the right direction. (Have mercy I am a beginner)

CodePudding user response:

If you want to get the floating-point division of two integers (as in - the result will be less than a whole number), you'll need to remember to cast one to a double/float/decimal so that it isn't classed as integer division which will cause "loss of fraction".

Since taxRate is an int (and should be), convert 100 to a double by replacing it with 100.0.

Your code should then work as expected, resulting in the correct tax rates.

taxPaid = annualSalary * (taxRate / 100.0);

CodePudding user response:

I would write some tests and around your calculations and break out those calculations into separate functions/methods.

This code repeats quite a bit:

 taxPaid = annualSalary * (taxRate / 100);
 netSalary = annualSalary - taxPaid;

So, break it out so it is testable.

Example:

public decimal CalcTaxPaid(decimal annualSalary, decimal taxRate)

Now just write some tests around that function. The IF statement becomes simpler as well.

Similar, for netSalary, could do the same. this is easier than debugging because the tests should show the behavior.

  •  Tags:  
  • c#
  • Related