# C# calculator

### Question

• I am working on using the below scenarios...just so you get an idea of the results I am wanting...need some idea of how to get these results. The && sign is not working (I get an error) not sure how to make these work so I can do the calculations needed. Basically it boils down to if the item price is less than 100 and the discount indicator sum is greater than 3, it is given a 20% discount, if price is between 100 and 149.99 and the discount indicator is given a 25% discount, however, if the item is above 149.99 and the sum is greater than 3, it gets a \$30.00 discount. If sum of discount indicators is less than 3 then it is a 0% discount. I have everything working except the below scenario and cannot figure it out....the value 69 is 1 for less than 100, 2 for 100 - 149.99 and 3 for above 149.99. Please help!!

if(sum1>3 and value69=1)

result20= .20

textBox20.Text = result20.ToString();

if (sum20 < 3)
result20 == 0;
textBox20.Text = result20.ToString();

if (sum20 > 3) and(value69 == 2);
result20 == 0.25;
textBox20.Text = result20.ToString();

if (sum20 > 3) and(value69== 3);
result20 == 30.0;
textBox20.Text = result20.ToString();

Can the word "and" be used here or what is the best way to set up this scenario?

Monday, April 22, 2013 3:05 PM

• before all code in block declare result20

`double result20 = 0;`

• Marked as answer by Tuesday, April 23, 2013 2:45 PM
Tuesday, April 23, 2013 12:56 PM

### All replies

• Use && operator instead of "and":

if(sum20 > 3 && value69 == 3)
{

}

Monday, April 22, 2013 3:12 PM
• 1. You should use && operator.

If you get an error - show us the error that you get.

2. You have an error:

`if(sum1>3 and value69=1)`

You need to have two == in order to compare in c#.

3. Your scenario looks like a classic case to use the switch statement.

Noam B.

Monday, April 22, 2013 3:16 PM
• Something like this may help.

if (sum20 < 3) {
result20 = 0;
} else {
if(value69 == 1) result20 = 0.20;
if(value69 == 2) result20 = 0.25;
if(value69 == 3) result20 = 30.0;
}
textBox20.Text = result20.ToString();

I assume that result20 should be set from value69 when it is 3 (your code deals with < and > but not =).

Is your last value (30.0) right (others are fractions)?

PS Noam's suggestion about switch is worth looking at - looks a good case for it.

Regards David R
---------------------------------------------------------------
The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones.
Object-oriented programming offers a sustainable way to write spaghetti code. - Paul Graham.
Every program eventually becomes rococo, and then rubble. - Alan Perlis
The only valid measurement of code quality: WTFs/minute.

• Edited by Monday, April 22, 2013 4:20 PM Added PS
Monday, April 22, 2013 4:19 PM
• This works except at the end textBox20.Text = reuslt20.ToString();

gets error Use of unassigned local variable 'result20'

Any suggestions?

Tuesday, April 23, 2013 12:53 PM
• before all code in block declare result20

`double result20 = 0;`