locked
Need a 2nd pair of eyes to help me see what I am missing RRS feed

  • Question

  • I have written my code however it is not calculating correctly.  In the fist if block the net pay is not calculating properly and in the 2nd if block the gross pay and net pay are not calculating properly.  Please help me see my mistake...

      static void Main(string[] args)
            {
                const double SINGLE_RATE = .22;
                const double MARRIED_RATE = .15;
                const double WIDOWED_RATE = .13;
                const double DIVORCED_RATE = .23;
                string maritalStatus;
                double hourlyRate = 0, hoursWorked = 0, regularPay = 0, overtimePay = 0, grossPay = 0, taxRate = 0, taxAmount = 0, netPay = 0;


                Console.WriteLine("Please enter in your hourly rate: ");
                hourlyRate = Convert.ToDouble(Console.ReadLine());
                Console.WriteLine("Please enter in your hours worked: ");
                hoursWorked = Convert.ToDouble(Console.ReadLine());
                Console.WriteLine("Please enter in your marital status (M)arried, (S)ingle, (W)idowed, (D)ivorced: ");
                maritalStatus = Console.ReadLine().ToUpper();

                switch (maritalStatus)

                {
                    case "M":
                        taxRate = MARRIED_RATE;
                        break;
                    case "S":
                        taxRate = SINGLE_RATE;
                        break;
                    case "W":
                        taxRate = WIDOWED_RATE;
                        break;
                    case "D":
                        taxRate = DIVORCED_RATE;
                        break;
                   
                }
                if (hoursWorked <= 40)
                {
                    grossPay = (hoursWorked * hourlyRate);
                    Console.WriteLine("Your gross pay is: " + grossPay.ToString("C"));
                    netPay = (grossPay - taxAmount);
                    Console.WriteLine("Your net pay is: " + netPay.ToString("C"));
                
                }
                {
                    taxAmount = (grossPay * taxRate);
                }

                if (hoursWorked > 40)
                {
                    regularPay = (hoursWorked * hourlyRate);
                    Console.WriteLine(value: "Your regular pay is: " + regularPay.ToString("C"));
                    overtimePay = (hoursWorked - 40) * hourlyRate * 1.5;
                    Console.WriteLine("Your overtime pay is: " + overtimePay.ToString("C"));
                    grossPay = (regularPay + overtimePay);
                    Console.WriteLine("Your gross pay is: " + grossPay.ToString("C"));
                    netPay = (grossPay - taxAmount);
                    Console.WriteLine("Your net pay is: " + netPay.ToString("C"));
                   

                }

                

                Console.WriteLine("Thank you for using our program");
                Console.ReadLine();
            }
        }
    }

    Saturday, May 19, 2018 7:52 AM

All replies

  •             if (hoursWorked <= 40)
                {
                    grossPay = (hoursWorked * hourlyRate);
                    Console.WriteLine("Your gross pay is: " + grossPay.ToString("C"));
                    netPay = (grossPay - taxAmount);
                    Console.WriteLine("Your net pay is: " + netPay.ToString("C"));
                
                }
                {
                    taxAmount = (grossPay * taxRate);
                }

                if (hoursWorked > 40)
                {
                    regularPay = (hoursWorked * hourlyRate);
                    Console.WriteLine(value: "Your regular pay is: " + regularPay.ToString("C"));
                    overtimePay = (hoursWorked - 40) * hourlyRate * 1.5;
                    Console.WriteLine("Your overtime pay is: " + overtimePay.ToString("C"));
                    grossPay = (regularPay + overtimePay);
                    Console.WriteLine("Your gross pay is: " + grossPay.ToString("C"));
                    netPay = (grossPay - taxAmount);
                    Console.WriteLine("Your net pay is: " + netPay.ToString("C"));
                 }

    May be you want something like

                if (hoursWorked <= 40)
                {
                    grossPay = (hoursWorked * hourlyRate);
                    taxAmount = (grossPay * taxRate);
                    netPay = (grossPay - taxAmount);
                    Console.WriteLine("Your gross pay is: " + grossPay.ToString("C"));
                    Console.WriteLine("Your net pay is: " + netPay.ToString("C"));
                }
    
                if (hoursWorked > 40)
                {
                    regularPay = (hoursWorked * hourlyRate);
                    overtimePay = (hoursWorked - 40) * hourlyRate * 1.5;
                    grossPay = (regularPay + overtimePay);
                    taxAmount = (grossPay * taxRate);
                    netPay = (grossPay - taxAmount);
                    Console.WriteLine(value: "Your regular pay is: " + regularPay.ToString("C"));
                    Console.WriteLine("Your overtime pay is: " + overtimePay.ToString("C"));
                    Console.WriteLine("Your gross pay is: " + grossPay.ToString("C"));
                    Console.WriteLine("Your net pay is: " + netPay.ToString("C"));
                }

    • Proposed as answer by Dolen Zhang Monday, May 21, 2018 4:01 AM
    Saturday, May 19, 2018 8:21 AM
  • You need to calculate taxAmount sepately in each "if" block, after you have calculated grossPay, but before you use it to calculate netPay.

    In the second "if", regularPay will be 40.0 * hourleyRate, not hoursWorked * hourleyRate.

    Saturday, May 19, 2018 9:26 AM
  • Your calculation of the taxamount is 5 code lines to late. 

    Success
    Cor

    Saturday, May 19, 2018 9:48 AM