 TechArena Community Problem with my IF ELSE IF statements in C++

#1
 KACY5 Member Join Date: Aug 2009 Posts: 54
Problem with my IF ELSE IF statements in C++

The below is my code. The problem is that I am not getting the cost correctly based on my conditions. It always displays the total cost something that I don't want. I want to specify some conditions based on which the cost and then the discount is to be calculated. But the code always displays some weird values. What modifications do I need to do in my code to get the conditional based total cost and discounts?

Code:
```cost=(cost * 100)
if (cost < 5)
cost=(cost * 100);

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

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

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

else if (cost > 30);
cost=(100 -(100 * 0.5));

discount = (100 - cost);

cout<<"Total cost: " << cost << " and discount: " << discount;```
#2
 Katty Member Join Date: May 2008 Posts: 2,008
Re: Problem with my IF ELSE IF statements in C++

Quote:
 else if (cost > 5 && cost < 10);
Your problem lies over here. You have used ";" at the end of the "else if" statement. This indicates the end of condition if the value of "cost" is in between 5 and 10, then the compiler has nothing to do. That is the reason why you are getting such answers. Just remove the semicolon and you will get the proper result.
#3
 fellah Member Join Date: May 2008 Posts: 685
Re: Problem with my IF ELSE IF statements in C++

Quote:
 cost=(cost * 100) if (cost < 5) cost=(cost * 100);
First and foremost problem lies over here, you are already multiplying the cost with 100 before implementing the conditions. Remove the first statement and also apply the changes that was said previously and then try to run the code. This time you should get the proper answers.

 Tags:

 Thread Tools Search this Thread Show Printable Version Email this Page Search this Thread: Advanced Search Similar Threads for: "Problem with my IF ELSE IF statements in C++" Thread Thread Starter Forum Replies Last Post Different statements in SQL query Kuna! Software Development 10 12-03-2011 04:02 PM Conditional statements in C sharp Vaikuntam Software Development 3 16-12-2010 08:36 AM How to Inject SQL Statements in PHP? DANIEL 602 Software Development 5 05-03-2010 03:46 AM Difference between DML statements and DDL statements Prashobh Mallu Software Development 5 11-01-2010 01:07 PM SQL statements with JSP blindsleeper Software Development 2 16-05-2009 09:54 PM

All times are GMT +5.5. The time now is 09:49 AM.