# Thread: Problem with my IF ELSE IF statements in C++

1. 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. Member
Join Date
May 2008
Posts
2,015

## Re: Problem with my IF ELSE IF statements in C++

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. Member
Join Date
May 2008
Posts
685

## Re: Problem with my IF ELSE IF statements in C++

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.

#### Posting Permissions

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