0

I am trying to do make a basic program in C# that calculates netPay and grossPay, however I am running into a little problem. I included a switch to set my taxRate to a const based off of a letter provided, after I added the switch into my program it says taxRate is an unassigned local variable. I am still very new to C# so I probably made a very simple mistake but for the life of me I cant find it. thanks in advance for the help.

        const int married = 15, single = 22, divorced = 23, widowed = 13;
        double payRate, hoursWorked, grossPay, netPay;
        double taxRate;
        char marStatus;



        Console.WriteLine("Please Enter Hourly Wages");
        payRate = int.Parse(Console.ReadLine());

        Console.WriteLine("Please Enter Hours Worked");
        hoursWorked = int.Parse(Console.ReadLine());

        Console.WriteLine("Please Enter Marital Status Letter: (M) Married (S) Single (D) Divorced (W) Widowed"); 
        marStatus =Convert.ToChar(Console.ReadLine());


        switch (marStatus)
        {
            case 'M':
                taxRate = married;
                break;
            case 'S':
                taxRate = single;
                break;
            case 'D':
                taxRate = divorced;
                break;
            case 'W':
                taxRate = widowed;
                break;
            default:
                Console.WriteLine("Invalid Input, Please Try Again.");
                break;
        }

        if (hoursWorked > 40)
        {grossPay =((hoursWorked-40)*(payRate*1.5))+(40*payRate);}
        else
        { grossPay = payRate * hoursWorked; }

        netPay = grossPay * taxRate;   // This is where I have the problem

        Console.WriteLine("Gross Pay=" +grossPay);
        Console.WriteLine("Net Pay=" +netPay);
        Console.WriteLine("xxx");

        Console.ReadLine();
Kilmark
  • 1
  • 1

1 Answers1

1

in the switch-case, if the defual case is met (no other case matches "marStatus"), then taxRate doesn't get asigned with a value. later you try to use this variable, with no value. this is the compilation error you're getting. asign a value to the variable.

Imbar M.
  • 1,074
  • 1
  • 10
  • 19