help with c++ problem.

a software company sells a package that retails for $99 . Quantity discounts are given according to this table.

Quantity              Discount
    10-19                             20%
    20-49                             30%
    50-99                             40%
    100 or more                    50%

Write a program that asks for the number of units sold and computes the total cost of the purchase.

Input Validation: Make sure the number of units is greater than 0.

Here is what I wrote out, tell me whats wrong with it.

#include <iostream>
using namespace std;

int main()
{
    int quantity, discount, totcost, pprice;
    pprice = 99;
    totcost = pprice * quantity * discount;

    cout << "How many units were sold? ";
    cin >> quantity;
    if (quantity == 10 && quantity <= 19)
        discount = 20;
    else if (quantity == 20 && quantity <= 49)
             discount = 30;
    else if (quantity == 50 && quantity <= 99)
             discount = 40;
    else if (quantity >= 100)
             discount = 50;
    else 
            discount = 0;
    cout << "The total cost of the units is $ " << totcost
         << endl;
    return 0;

Recommended Answers

All 3 Replies

First, your calculation isn't correct. Think about the discount calculation.

Second, you can't calculate the cost before you know the quantity and discount.

Third, your conditionals checking the quantity need to be reconsidered. They won't work as desired but only require a small change.

Finally, you aren't validating the input per your instructions.

You also need to consider the following:
1) The results might not be integers
2) The initial equation uses discount and I think you're looking for 100% - discount
3) Discount should probably be in the range 0.0 - 1.0 or you need to divide by 100 to correct the math.
4) As written, orders that are not exactly 10, 20, 50, or 100 or larger will be computed as free (i.e. 0)

tot_cost=pprice*quantity;
if (quantity >= 10 && quantity <= 19) {
discount = tot_cost * .2; /*percent to decimal conversion: 20%/100% = .2*/ 
discounted_cost = tot_cost - discount; }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.