tags:

views:

1020

answers:

4

I don't understand if the calculation for netPay = grossPay - (fedtax withholding + social security tax withholding). Are my calculations correct within the program? Dealing with such in editedTax?? If someone could help I'd appreciate it.

More info: When I display netPay within an output, I receive a runtime error, where I couldn't convert the negative to currency with {0:c2}.

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication5
{
    class Program
    {
        static void Main(string[] args)
        {
            string empName;
            string userInput;

            double netPay;
            double editedTax1;
            double grossPay;
            double editedTax2;
            double hrsWorked;
            double ovtWorked;
            double payRate;

            const double FED_TAX = .28;
            const double SS_TAX = 7.65;




            // step 1
            Console.WriteLine("       WEEKLY PAYROLL INFORMATION");

            // step 2
            Console.WriteLine("       --------------------------");

            // step 3
            Console.Write("\n       Please enter the employer's name: ");
            empName = Console.ReadLine();

            //step 4
            Console.Write("\n       Please enter the number of hours worked this week: ");
            userInput = Console.ReadLine();
            hrsWorked = Convert.ToDouble(userInput);

            // step 5
            Console.Write("\n       Please enter the number of OVERTIME HOURS worked this week: ");
            userInput = Console.ReadLine();
            ovtWorked = Convert.ToInt32(userInput);

            // step 6
            Console.Write("\n       Please enter employee's HOURLY PAY RATE: ");
            userInput = Console.ReadLine();
            payRate = Convert.ToDouble(userInput);

            // step 7
            grossPay = (hrsWorked * payRate + ovtWorked * 1.5 * payRate);

            // step 8
            editedTax1 = FED_TAX * grossPay;

            // step 9
            editedTax2 = SS_TAX * grossPay;

            // step 10
            netPay = editedTax1 + editedTax2 - grossPay;

            // step 11
            Console.WriteLine("\n\n       The weekly payroll information summary for: " + empName);

            Console.WriteLine("\n       Gross pay:                             {0:C2}    ", grossPay);

            // step 12
            Console.WriteLine("       Federal income taxes witheld:          {0:C2}      ", editedTax1);
            Console.WriteLine("       Social Security taxes witheld:         {0:C2}    ", editedTax2);
            Console.WriteLine("       Net Pay:                               {0:C2}", netPay);


        }
    }
}
+2  A: 

netPay is assigned the opposite value in the code as compared to your description below.
I don't see any syntax errors or anything.

What is the problem you're having? What are some of the things you've tried?

recursive
A: 

I'm not sure exactly what you're asking for, but simple substitution in the assignment statements yields the following formulas:

Since

editedTax1 = FED_TAX * grossPay;

editedTax2 = SS_TAX * grossPay;

netPay = editedTax1 + editedTax2 - grossPay;

then

netPay = FED_TAX * grossPay + SS_TAX * grossPay - grossPay;

meaning

netPay = grossPay * (FED_TAX + SS_TAX - 1);

so something seems a little off here...


Are you sure you don't want

netPay = grossPay - (editedTax1 + editedTax2);

instead of

netPay = editedTax1 + editedTax2 - grossPay;

This seems to match what you're looking for as

netPay = grossPay - (FED_TAX * grossPay + SS_TAX * grossPay);

or

netPay = grossPay * (1 - (FED_TAX + SS_TAX));

...unless I'm missing something, of course.


Edit: I was missing something. Your tax constants are a percent, but you're not dividing by 100 when you do calculations with them. You have two options:

  1. Divide by 100 when you use the values in a calculation, like:

    editedTax1 = (FED_TAX / 100) * grossPay;

  2. Store the constants as the decimal representation, not a percent, like:

    const double FED_TAX = .0028;

lc
i want the 1st one, netPay = grossPay - (editedTax1 + editedTax2); but when I display it within an output, i a runtime error, where i couldn't convert the negative to currency with {0:c2}
Brent
those two statements should give different answers correct? particularly on different input?
Brent
Those two statements are exactly inverse and will give different answers unless grossPay is zero. It's the same as 3 - 2 = 1 and 2 - 3 = -1.
lc
Secondly, I had another look through your code and found the reason for your negative numbers. Answer edited.
lc
+2  A: 

The reason you're getting a negative number in the calculation is because your SS_TAX is 7.65. I think the number you want is 0.0765.

Jim Mischel
I wish there were a downvote option on posts tagged as 'homework'. But you should win the badge for homework saviour, Jim.
p.campbell
well i'm sorry, if i have no other options in furthering my knowledge in programming.
Brent
@Philoushka: We've all been in situations where we're too close to the problem to see the obvious, and need a little help. It's not like he was asking us to do his assignment for him.
Jim Mischel
We've had this problem *so many* times where I work... If fed tax is 28%, it is correctly 0.28. how come the social security tax, then, is 765% ? If it's 7.65%, then the number you want is indeed 0.0765.
configurator
A: 

I have tested this code on my machine and it works correctly:

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication5
{
    class Program
    {
        static void Main(string[] args)
        {
            const double FEDERAL_TAX_RATE= 0.28;
            const double SOCIAL_SECURITY_RATE = 0.0765;  // I am assuming the 7.65 was supposed to be 7.65%... therefore it should be 0.0765

            Console.WriteLine("       WEEKLY PAYROLL INFORMATION");
            Console.WriteLine("       --------------------------");
            Console.Write("\n       Please enter the employer's name: ");
            string empName = Console.ReadLine();
            Console.Write("\n       Please enter the number of hours worked this week: ");
            double hrsWorked = Convert.ToDouble(Console.ReadLine());
            Console.Write("\n       Please enter the number of OVERTIME HOURS worked this week: ");
            double ovtWorked = Convert.ToInt32(Console.ReadLine());
            Console.Write("\n       Please enter employee's HOURLY PAY RATE: ");
            double payRate = Convert.ToDouble(Console.ReadLine());
            double grossPay = CalculateGrossPay(hrsWorked, payRate, ovtWorked);
            double federalTaxWithheld = CalculateTax(grossPay, FEDERAL_TAX_RATE);
            double socialSecurityWithheld = CalculateTax(grossPay, SOCIAL_SECURITY_RATE);
      double netPay = CalculateNetPay(grossPay, federalTaxWithheld + socialSecurityWithheld);

            Console.WriteLine("\n\n       The weekly payroll information summary for: " + empName);
            Console.WriteLine("\n       Gross pay:                             {0:C2}    ", grossPay);
            Console.WriteLine("       Federal income taxes witheld:          {0:C2}      ", federalTaxWithheld);
            Console.WriteLine("       Social Security taxes witheld:         {0:C2}    ", socialSecurityWithheld);
            Console.WriteLine("       Net Pay:                               {0:C2}", netPay);

      Console.ReadLine();  // You don't need this line if your running from the command line to begin with
        }

     static double CalculateGrossPay(double HoursWorked, double PayRate, double OvertimeHoursWorked)
     {
      return PayRate * (HoursWorked + 1.5 * OvertimeHoursWorked);
     }

     static double CalculateTax(double GrossPay, double TaxRate) 
     {
      return GrossPay * TaxRate;
     }

     static double CalculateNetPay(double GrossPay, double TaxAmount)
     {
      return GrossPay - TaxAmount;
     }
    }
}

Since it looks as though this is your first programming course, I'll offer you some pointers that they probably will not emphasize in your class:

  • Use descriptive variable names. If you notice yourself putting a number after your variable name, it can probably done a different and more readable way!
  • Utilize functions, they bundle common tasks in one section of code and increase readability significantly.
  • You may want to add some exception handling or validation to this code. For example, what if you accidentally passed -1 into OverTimeHours?

I understand this might not matter at this point in your programming journey, but it's always good to start off using coding techniques that make your code more readable and less confusing, especially for people who may have to maintain it in the future.

John Rasch