Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
1.75/5 (4 votes)
See more:
This sh*t has been solved.

ok?
Posted
Updated 22-Aug-12 16:08pm
v4
Comments
Prabhakaran Soundarapandian 9-Aug-12 23:57pm    
You just posted your code...improve your question what is the scenario and what loop validation you want..then only we could help you..
AmitGajjar 10-Aug-12 0:07am    
what you mean by validating if else ????
Vani Kulkarni 10-Aug-12 0:12am    
What is the issue you are facing?
AmitGajjar 10-Aug-12 0:18am    
your code is correct, nothing to do.
AmitGajjar 10-Aug-12 0:33am    
See my solution 4 for your answer. Mark it as answered if it helps you.

thanks.

As I can see your if..else statement is incorrect. You should validate it using >= & <= operators both.
Try this:
C#
//Determine pay rate and tax rates based on entered data
if (annual_salary >= 0 && annual_salary <= 16500)
{
    tax_rate = 11.32F;
    pay_rate = 8.68F;
}


[EDIT]:
Thank you @@Amitgajjar to suggest me.
Hi I executed your code it's working perfectly. For error checking use try..catch blocks.
Use this:
C#
//Determine pay rate and tax rates based on entered data
try{
    if (annual_salary >= 0 && annual_salary <= 16500)
    {
        tax_rate = 11.32F;
        pay_rate = 8.68F;
    }
}catch(Exception ex)
{
    Console.WriteLine(ex.Message);
}


Refer the links for more information:
try-catch (C# Reference)[^]
try-catch-finally (C# Reference)[^]

--Amit
 
Share this answer
 
v3
Comments
malakas1821 10-Aug-12 0:02am    
First of all thanks for your quick answer.
This is exactly how I did it , but my programming teacher told me to do it the way it is on the first post.
AmitGajjar 10-Aug-12 0:09am    
Sorry _Amy but in your code there is no need for first Condition in "else if" statements. because it is already checked by it's previous condition.
_Amy 10-Aug-12 0:11am    
To prevent fall through I used that. :)
AmitGajjar 10-Aug-12 0:18am    
suggest you to Improve your solution otherwise someone may downvote it. because it is irrelevant.
_Amy 10-Aug-12 0:33am    
Try my updated answer. :)
No. Whatever your teacher said, its right. I would suggest you to go ahead with your original post..

When you are having it in sequence, like this.
C#
if (annual_salary >= 0 && annual_salary <= 16500)
            {
                tax_rate = 11.32F;
                pay_rate = 8.68F;
            }
            else if (annual_salary <= 19500)
            {
                tax_rate = 15.14F;
                pay_rate = 10.26F;
            }



its same as

C#
if (annual_salary >= 0 && annual_salary <= 16500)
{
    tax_rate = 11.32F;
    pay_rate = 8.68F;
}
else if (annual_salary >= 16500 && annual_salary <= 19500 )
{
    tax_rate = 15.14F;
    pay_rate = 10.26F;
}


For eg, when i pass 17000, definitely it will take tax rate as 15.14F, since it will fail first if loop and enters else if.

If you want to test the complete code,

use the following values and run once and test your code.

Input:Annual_salary Expected:Tax_rate
14000 11.32F
17000 15.14F
21000 22.65F
32000 27.1F
34000 30.92F
45000 35.72F
78000 40.72F
91000 50.52F
 
Share this answer
 
v2
Comments
malakas1821 10-Aug-12 0:12am    
Is there anything else that I can add or is it fine as it is ?
Santhosh Kumar Jayaraman 10-Aug-12 0:14am    
The if else loop is fine.It will definitely work
malakas1821 10-Aug-12 0:20am    
Ok , thanks . can you help me with error checking the data ?
Santhosh Kumar Jayaraman 10-Aug-12 0:22am    
i am not sure what you want from me?
malakas1821 10-Aug-12 0:25am    
to make errors come up if the entering data are not correct
Hi,

You need to add some exception handling at many places.

Example 1: User should not allowed to enter annual_salary below 0.
So your code would be like,

C#
If(annual_salary<=0)
{
throw new ArgumentOutOfRangeException(); //// Here you can even prompt some message. if you are developing API then you should throw Exception to handled from client.
}


Example 2: User should not enter Empty name.
the code would be like,

C#
if (string.IsNullOrEmpty(full_name))
{
throw new ArgumentNullException(); //// Here you can even prompt to enter correct name.
}


This way so many validation can be added in your application.

Best of luck

Thanks
-Amit Gajjar.
 
Share this answer
 
Comments
malakas1821 10-Aug-12 0:41am    
I havent learned these yet. What my professor told me was that "Think about using if statements to check. Also use tryparse to test for integers."
Can you give me an example with that ?
AmitGajjar 10-Aug-12 0:49am    
The reason your teacher said that, user may enter incorrect integer value may be some string. you need to handle it using int.TryParse
AmitGajjar 10-Aug-12 0:51am    
you are using int.parse this will fail if string is entered by the user. so instead use int.TryParse.
malakas1821 10-Aug-12 0:52am    
can you give me one example on how can I use it ?
AmitGajjar 10-Aug-12 0:55am    
check MSDN article you and find example there int.TryParse
A similar code snippet was posted in the Horrors recently. I think the correct answer here is the same as it was there: a list of bands and a simple lookup into them:

public class Band {
 public float UpperBound, TaxRate, PayRate;
 public Band(float upperBound, float taxRate, float payRate) { 
  UpperBound = upperBound; TaxRate = taxRate; PayRate = payRate;
 }
}

class MainClass {
 private static readonly Band[] bands = {
  new Band(16500, 11.32F, 8.68F),
  new Band(19500, 15.14F, 10.26F),
  // etc
  new Band(89500, 40.72F, 47.12F),
  new Band(float.Infinity, 50.52F, 55.67F) // Infinity matches any finite salary
 );

 void YourMethod(){
  // All the code that gets stuff off the console, etc
//Annual Salary data
  Console.Write("Enter your annual salary >");
  annual_salary = int.Parse(Console.ReadLine());
              
  Band salaryBand = bands.First(b => b.UpperBound >= annual_salary);
  tax_rate = salaryBand.TaxRate; pay_rate = salaryBand.PayRate;

  // Calculate gross pay
  gross_pay = hours_worked * pay_rate;
  // ... etc
 }
}


And yes you should probably guard against a salary less than zero, otherwise you'll get the first one, which might not be what you want.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900