ID:1334338   Jul 25 2013, 6:54 am (Edited on Jul 25 2013, 7:01 am) (See the best response by Jp.) Code: ```double applyDiscounts(int orderAmt);int main(){ const double tier0Cost = 0.95; //normal price per product const double tier1Cost = 0.9; //cost per product over 1,000th const double tier2Cost = 0.8; //cost per product over 5,000th double final_cost = 0.0; int orderAmt = 0; cout<<"Enter order amount: "; cin>>orderAmt; final_cost = applyDiscounts(orderAmt); cout<<"Final cost is "< 1000 && orderAmt <= 5000) { total += 1000*tier0Cost; total += (orderAmt-1000)*tier1Cost; } else if(orderAmt > 5000) { total += 1000*tier0Cost; total += 4000*tier1Cost; total += (orderAmt-5000)*tier2Cost; } return total;} ``` Problem description: Above is the snippet I use to accomplish the following task. Applying different discount rates to different amounts of ordered product. As of right now, I Just use a chain of if/else if/else, but I can't help but think there is a better way of handling this scenario. How the discounts work, is that for the first 1,000 product ordered, the normal price is applied. For every product ordered over 1,000 (so #1,001 and above), the tier 1 cost is applied, and so on. For example, if I were to order 1,500 product, 1,000 of it would cost the normal 0.95 per, while the final 500 of it would cost 0.9 per, for a total of (1000*0.95)+(500*0.9)=(950)+(450)=1,400 for the final cost. EDIT There may be a couple minor syntax errors from my writing this up real quick. This isn't the exact program, but instead just something I wrote just now to get the idea across.
 Jul 25 2013, 7:12 am You got it down pretty nice, I doubt there's another way to do it that's less complicated than what you have here.
 Jul 25 2013, 7:35 am Best response ```total += std::min(1000, amount) * tier0cost + std::min(4000, std::max(0, amount - 1000)) * tier1cost + std::max(0, amount - 5000)*tier2cost ``` But what you've got is much, much more readable. Also presumably you're sending std::endl after cout-ing and have a using clause and so on, but that's minor.
 Jul 25 2013, 8:10 am I fail to see how that even compiles, but that's not the point. If you define two arrays and just use loop to search through it'll be possible to apply this to any number of discounts.
 Jul 25 2013, 12:48 pm In response to Zaoshi How would this not compile? Obviously I excluded the #include and using namespace std;, but that's not the point of the post. Unless you're referring to the fact that I put the const variables inside main() instead of outside main() like a smarter person would haha.
 Jul 25 2013, 2:04 pm In response to Jp I actually like your idea of using min/max instead of a chain of if/else if/else statements. However, I spread each one out into its own line rather than what you did, just to make it 100x easier to read. I've never been a fan of squishing as much as possible onto a single line of code for the sake of saving lines of code haha. ```//apply normal pricetotal += min(amount, 1000)*tier0Cost;//apply tier 1 discounttotal += min(4000, max(0, amount-1000))*tier1Cost;//apply tier 2 discounttotal += max(0, amount-5000)*tier2Cost; ```