here is the error I am getting

_Default.CalculateFederalTax(double, string)':not all code paths return a value



code:


double CalculateFederalTax(double grossPay, string taxStatus)
{ // Calculate Federal Tax method begins


// check for the Federal Tax status (Single)
if (taxStatus == "S")
{
if (grossPay < 240)
return grossPay * 0.12;
else if (grossPay < 480 && grossPay > 240)
return grossPay * 0.18;
else if (grossPay < 720 && grossPay > 480)
return grossPay * 0.25;
else if (grossPay >= 720)
return grossPay * 0.28;
}
else   // Federal Tax status (Married)
{
if (grossPay < 240)
return grossPay * 0.12;
else if (grossPay < 480 && grossPay > 240)
return grossPay * 0.17;
else if (grossPay < 720 && grossPay > 480)
return grossPay * 0.25;
else if (grossPay >= 720)
return grossPay * 0.30;
}



} // Calculate Federal Tax method ends

Thanks in advance

I got it to work, dont worry about it guys

double CalculateFederalTax(double grossPay, string taxStatus)
{ 
if (taxStatus == "S")
{
if (grossPay < 240)
return grossPay * 0.12;
else if (grossPay < 480 && grossPay > 240)
return grossPay * 0.18;
else if (grossPay < 720 && grossPay > 480)
return grossPay * 0.25;
else if (grossPay >= 720)
return grossPay * 0.28;
}
else
{
if (grossPay < 240)
return grossPay * 0.12;
else if (grossPay < 480 && grossPay > 240)
return grossPay * 0.17;
else if (grossPay < 720 && grossPay > 480)
return grossPay * 0.25;
else if (grossPay >= 720)
return grossPay * 0.30;
}
return 0;
}

don't worry, it won't return 0 as you return in your 'if' and 'else'

ok if u use if or switch in your code that means there are multiple alternatives and on each alternative the method must return a double, you must preserve a return result for each alternative to avoid this kind of problem
I give an example:

if(condition)
{
instruction1;
return Variable;
Break;
}
esle
{
return null;
MessageBox.Show("The variable can't be calculated");
}
another example
switch(Variable1)
{
case alternative1 :
{
instruction1;
retrun Variable
}
case alternative2 :
{
instruction2;
retrun Variable
}
default:
retrun null;
MessageBox.Show("Something wrong!!!");
}

even when u use an try{}catch(Exception caught){} u have the same problem if u don't proceed like the code bellow

try
{
instruction
return Variable;
}
catch(Exception caught)
{
return null;
MessageBox.Show("Something wrong!!!!!");
}

Help please, having trouble using methods.

The error keeps telling me that the regHours, grossPay, rate and so on does not exist in the context.

public class Program
{
    public static void Main()
    {
    }
    public static void GetInfo(string side)
    {
        string fname;
        Console.Write("Please enter the employee's name: ");
        fname = Console.ReadLine();

        string inputValue;
        int regHours,
            ovtHours;
        Console.Write("Please enter the number of regular hours: ");
        inputValue = Console.ReadLin e();
        regHours = int.Parse(inputValue);
        Console.Write("Please enter the number of overtime hours: ");
        inputValue = Console.ReadLine();
        ovtHours = int.Parse(inputValue);

        string inputRate;
        int rate;
        Console.Write("Please enter the employees pay rate: ");
        inputRate = Console.ReadLine();
        rate = int.Parse(inputRate);
    }
    public static double Calculator()
    {            
        double grossPay = regHours * rate + ovtHours * 1.5 * rate;
        double fedInTax = grossPay * .28;
        double socSecTax = grossPay * .0765;
        double netPay = fedInTax + socSecTax - grossPay;
    }
    public static void DisplayResults()
    {
        System.Console.Out.WriteLine("\nPaycheck breakdown for " + fname);
        System.Console.Out.WriteLine("\nGross pay is equal to: " + grossPay.ToString("C"));
        System.Console.Out.WriteLine("\nFederal income tax withheld is equal to: " + fedInTax.ToString("C"));
        System.Console.Out.WriteLine("\nSocial security tax withheld is equal to: " + socSecTax.ToString("C"));
        System.Console.Out.WriteLine("\nNet pay is equal to:$ " + netPay.ToString("C"));


        System.Console.Out.WriteLine("Program designed by Dale Brooks");

You have declared the variable
int regHours,ovtHours in GetInfo. the scope of the variable is upto the function getinfo. but you used the same variable in the function Calculator.

Inorder to use the variable please declare as a private variable in the class common to both function

Regards
razool

i think this should be a proper answer of your problem.

public class Program
{
    public static void Main()
    {
          //Suppose the string we pass is abc as parameter
            getInfo("abc");
           Console.ReadLine();
    }
    public static void GetInfo(string side)
    {
        string fname;
        Console.Write("Please enter the employee's name: ");
        fname = Console.ReadLine();

        string inputValue;
        int regHours,
            ovtHours;
        Console.Write("Please enter the number of regular hours: ");
        inputValue = Console.ReadLin e();
        regHours = int.Parse(inputValue);
        Console.Write("Please enter the number of overtime hours: ");
        inputValue = Console.ReadLine();
        ovtHours = int.Parse(inputValue);

        string inputRate;
        int rate;
        Console.Write("Please enter the employees pay rate: ");
        inputRate = Console.ReadLine();
        rate = int.Parse(inputRate);
        Calculator(fname,regHours,rate,ovtHours);
    }
    public static void Calculator(string fname,int regHours,int rate,int ovtHours)
    {            
        double grossPay = regHours * rate + ovtHours * 1.5 * rate;
        double fedInTax = grossPay * .28;
        double socSecTax = grossPay * .0765;
        double netPay = fedInTax + socSecTax - grossPay;
        DisplayResults(fname,grossPay,fedInTax,socSecTax,netPay);
    }
    public static void DisplayResults(string fname,double grossPay,double fedInTax,double socSecTax,double netPay)
    {
        System.Console.Out.WriteLine("\nPaycheck breakdown for " + fname);
        System.Console.Out.WriteLine("\nGross pay is equal to: " + grossPay.ToString("C"));
        System.Console.Out.WriteLine("\nFederal income tax withheld is equal to: " + fedInTax.ToString("C"));
        System.Console.Out.WriteLine("\nSocial security tax withheld is equal to: " + socSecTax.ToString("C"));
        System.Console.Out.WriteLine("\nNet pay is equal to:$ " + netPay.ToString("C"));


        System.Console.Out.WriteLine("Program designed by Dale Brooks");
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.