If else then
 CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

1. Junior Member
Join Date
Feb 2009
Posts
18

## If else then

It doesn't display the cost correctly. It just calculates the total x without taking into account the discount. What would I have to change/add to get the total cost after it calculates the discount?

cout<<"Total cost " << cost;

cost=(cost * 50);
discount = (50 - cost);

if (cost < 5)
cost=(cost * 50);

else if (cost > 5 && cost < 10);
cost=(50 - (50 * 0.2));

else if (cost > 10 && cost < 20)
cost=(50 - (50 * 0.3));

else if (cost > 20 && cost < 30)
cost=(50 - (50 * 0.4));

else if (cost > 30);
cost=(50 -(50 * 0.5));
Last edited by Guru_Kid; September 1st, 2009 at 12:32 AM.

2. ## Re: If else then

Originally Posted by Guru_Kid
cout<<"Total cost " << cost;

cost=(cost * 50);
discount = (50 - cost);

if (cost < 5)
cost=(cost * 50);

else if (cost > 5 && cost < 10);
cost=(50 - (50 * 0.2));

else if (cost > 10 && cost < 20)
cost=(50 - (50 * 0.3));

else if (cost > 20 && cost < 30)
cost=(50 - (50 * 0.4));

else if (cost > 30);
cost=(50 -(50 * 0.5));
Although I don't have time to fully understand your code, I may have found your problem. here:

Code:
```else if (cost > 5 && cost < 10); //Adding this semi-colon makes the if do nothing
cost=(50 - (50 * 0.2)); //this line always happens, it is not in the if```
My recommendation is to ALWAYS use brackets {} after an if/while/for, no exception. This will avoid you hours on end of headaches like this.

Also, try to avoid code like this:
Code:
`cost > 10 && cost < 20`
and prefer
Code:
`(cost > 10) && (cost < 20)`
in this case, it is alright not to put the parenthesis, but one day you (or someone reading your code) will mess up the precedence. Also, it is clearer.

3. Junior Member
Join Date
Feb 2009
Posts
18

## Re: If else then

Thanks. I edited some of the things you suggested. However, the discount part of it still isn't working. I updated my first post.

4. ZuK
Senior Member
Join Date
Oct 2002
Location
Austria
Posts
1,284

## Re: If else then

You are calculating the total_cost before calculating the discount.
and your if / else can be a lot sinpler

Code:
```if (x < 10)
discount=(0.0);    // less then 10 no discount
else if (x < 20)       // not < 10 and < 20
discount=(0.2);
else if ( x < 40 )     // not < 20 and < 40
discount=(0.3);
else if (x < 50)
discount=(0.4);
else                   // 50 and more
discount=(0.5);

value=(cost*discount);
total_cost= (cost - value);```
Kurt

5. Junior Member
Join Date
Feb 2009
Posts
18

## Re: If else then

What would I have to change/add to get the total cost after it calculates the discount?

6. ## Re: If else then

Originally Posted by ZuK
You are calculating the total_cost before calculating the discount.
and your if / else can be a lot sinpler

Code:
```if (x < 10)
discount=(0.0);    // less then 10 no discount
else if (x < 20)       // not < 10 and < 20
discount=(0.2);
else if ( x < 40 )     // not < 20 and < 40
discount=(0.3);
else if (x < 50)
discount=(0.4);
else                   // 50 and more
discount=(0.5);

value=(cost*discount);
total_cost= (cost - value);```
Kurt

My guess is that you declared "discount" as an integer (ie int/short/char), so it is always 0 (0.5 converts to 0 in integers).
try using a floating point type (ie double, for example), and your problems should be fixed.

int: with x=20 then Result is 0
float: with x=20 then Result is 35

7. Junior Member
Join Date
Feb 2009
Posts
18

## Re: If else then

In my original post, I posted that it was declared as double.

I'm not sure how you got 35. When I entered 20, I result is 500 (half * ( original x *50))

8. Elite Member Power Poster
Join Date
Jan 2006
Location
Singapore
Posts
6,731

## Re: If else then

What is your current code, keeping in mind ZuK's post #4? Remember to post your well indented code in [code][/code] bbcode tags.

Originally Posted by monarch_dodra
Also, try to avoid code like this:
On the other hand, superfluous parentheses make it more difficult to read code. The problem is determining just when they are superfluous and when they are beneficial despite being unnecessary, and that determination varies with the reader.
Last edited by laserlight; August 31st, 2009 at 06:54 AM.

9. Junior Member
Join Date
Feb 2009
Posts
18

## Re: If else then

Updated the first post
Last edited by Guru_Kid; August 31st, 2009 at 08:10 AM.

10. Elite Member Power Poster
Join Date
Jan 2006
Location
Singapore
Posts
6,731

## Re: If else then

You have a bug here:
Code:
```else
if (x > 50);
discount=(0.5);```
It should be:
Code:
```else if (x > 50)
discount = 0.5;```
or simply:
Code:
```else
discount = 0.5;```
Otherwise discount is always set to 0.5.

Also, this looks strange:
Code:
```if (x < 10)
discount=(x * 50);```
Notice that in my code snippets I am indenting your code. You should indent your own code.

EDIT:
Another thing: your use of || is not just unnecessary, but incorrect. It would be more correct as &&, but then you would have errors on boundary conditions (e.g., if x == 10). Just test for x less than the given number.
Last edited by laserlight; August 31st, 2009 at 07:08 AM.

11. Elite Member Power Poster
Join Date
Nov 2003
Location
Florida
Posts
12,518

## Re: If else then

else if (x >30 || x <40)

Let's say x is 10. That statement will evaluate true because x < 40.

Let's say x is 110. That statement will still evaluate true because x > 30.

So, aside from syntax errors, you'll need to get your logic straightened out to make it work. Please give it a little thought before just asking how to fix he logic.

12. Junior Member
Join Date
Feb 2009
Posts
18

## Re: If else then

Originally Posted by laserlight
You have a bug here:
I changed what you suggested.

My program still isn't coding correctly. It doesn't calculate the discount. Changed it a little.

Code:
```double cost;
double total_cost;
double value;
int x;
float discount;

cout<<"Enter the number of units sold: ";
cin>> x;

cost=(x * 50);

if (x < 10)
discount=(0);

else if (x > 50);
discount=(0.5);

value=(cost*discount);
total_cost= (cost - value);

cout<<"Total cost " << total_cost;```

13. Elite Member Power Poster
Join Date
Jan 2006
Location
Singapore
Posts
6,731

## Re: If else then

You still have the extra semi-colon.

14. Elite Member Power Poster
Join Date
Nov 2003
Location
Florida
Posts
12,518

## Re: If else then

Originally Posted by Guru_Kid

Code:
```cost=(x * 50);

discount=(0);

discount=(0.5);

value=(cost*discount);
total_cost= (cost - value);```
You don't need any of those parenthesis and I tole you a couple of posts ago why your discount calculation won't work.

15. Junior Member
Join Date
Feb 2009
Posts
18

## Re: If else then

Solved. Thanks x)

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•