2 Replies - 640 Views - Last Post: 16 October 2009 - 10:16 AM Rate Topic: -----

#1 autobotkilla  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 14-October 09

If/Else help

Post icon  Posted 16 October 2009 - 10:05 AM

so this is my second post, sorry if this is something that someone else has asked help with. but im working on this program, in theory it seems very simple but im very new to the C# stuff. the program is supposed to calculate tax withholdings based on what the user makes. i keep getting an error in the if else statement and i cant figure out what im doing wrong.

using System;
public class Payroll

{
	
	
		public static void Main()
		{
			double grossPay, 
				   hoursWorked, 
				   netPay, 
				   taxRate, 
				   hourPay;

			string grossPayAsString, 
				   hoursWorkedAsString, 
				   netPayAsString, 
				   taxRateAsString, 
				   hourPayAsString;

			const Double tax1 = .10, 
						 tax2 = .12;

			
			Console.WriteLine("Enter hourly pay: ");
			hourPayAsString = Console.ReadLine();
			hourPay = Convert.ToDouble(hourPayAsString);

			Console.WriteLine("Enter hours worked: ");
			hoursWorkedAsString = Console.ReadLine();
			hoursWorked = Convert.ToDouble(hourPayAsString);

			grossPay = hourPay * hoursWorked;

			if (grossPay < 300.00) 
			{
				taxRate = grossPay * tax1;
			}	
				
				 else (grossPay >= 300.00) 
			{   
				taxRate = grossPay * tax2;
			}   
						   
			
			netPay = grossPay - taxRate;
			

				Console.WriteLine("Gross Pay: {0}", grossPay.ToString("C"));
				Console.WriteLine("Tax withholding: {1}", taxRate.ToString("C"));
				Console.WriteLine("Net pay: {2}", netPay.ToString("C"));

		   
			Console.ReadLine();

		}


}



Is This A Good Question/Topic? 0
  • +

Replies To: If/Else help

#2 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1012
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: If/Else help

Posted 16 October 2009 - 10:14 AM

You are trying to check a second condition in your else statement, one which you don't need to do. If it's not less than 300, it must be over 300 :)

if (grossPay < 300.00) {
	taxRate = grossPay * tax1;
} else  {   
	taxRate = grossPay * tax2;
}


If you really needed to check another condition (lets say the rate is different for less than 300, 300 to less than 600, and 600 or more, you'd write it like this:

if (grossPay < 300.00) {
	taxRate = grossPay * tax1;
} else if (grossPay < 600.00) {  // It's 300 or more or we wouldn't be here
	taxRate = grossPay * tax2;
} else {  // It's 600 or more
	taxRate = grossPay * tax3;
}

Was This Post Helpful? 0
  • +
  • -

#3 thehat  Icon User is offline

  • awake ? web();
  • member icon

Reputation: 106
  • View blog
  • Posts: 951
  • Joined: 28-February 08

Re: If/Else help

Posted 16 October 2009 - 10:16 AM

You shouldn't put a condition after your else. If you want to test multiple times you use if else. Else is a catch all for when you previous tests have failed.

if (grossPay < 300.00)
{
   taxRate = grossPay * tax1;
}	
   else if (grossPay >= 300.00)
{  
   taxRate = grossPay * tax2;
}



Edit: too slow!

This post has been edited by thehat: 16 October 2009 - 10:17 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1