
August 31st, 2009, 01:05 AM
#1
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.

August 31st, 2009, 03:54 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 semicolon 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.

August 31st, 2009, 05:22 AM
#3
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.

August 31st, 2009, 05:48 AM
#4
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

August 31st, 2009, 06:22 AM
#5
Re: If else then
What would I have to change/add to get the total cost after it calculates the discount?

August 31st, 2009, 06:25 AM
#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
Please submit a short but COMPLETE example of your code. It makes our lives easier, and we can help you better.
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.
I tried your example with:
int: with x=20 then Result is 0
float: with x=20 then Result is 35

August 31st, 2009, 06:34 AM
#7
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))

August 31st, 2009, 06:51 AM
#8
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.

August 31st, 2009, 06:58 AM
#9
Re: If else then
Last edited by Guru_Kid; August 31st, 2009 at 08:10 AM.

August 31st, 2009, 07:04 AM
#10
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.

August 31st, 2009, 07:21 AM
#11
Re: If else then
Think about your logic here.
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.

August 31st, 2009, 07:45 AM
#12
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;

August 31st, 2009, 08:03 AM
#13
Re: If else then
You still have the extra semicolon.

August 31st, 2009, 08:06 AM
#14
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.

August 31st, 2009, 08:10 AM
#15
Posting Permissions
 You may not post new threads
 You may not post replies
 You may not post attachments
 You may not edit your posts

Forum Rules

Click Here to Expand Forum to Full Width
This is a Codeguru.com survey!
