Coffee Sales

For this assignment, write a program that calculates a discount for buying certain quantities of coffee. Consider the following scenario:

A coffee company sells a pound of coffee for $12.99. Quantity discounts are given according to the table below.

Quantity Discount
5-9 5%
10-19 10%
20-29 15%
30 or more 20%
Write a program that asks for the number of pounds purchased and computes the total cost of the purchase. Make sure the output formatting is appropriate for the values provided. Be sure to include comments throughout your code where appropriate

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
//Charles Blackwell CIS211 Module 4 Assignment
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
//main function
int main() {
	double firstlevel = 0.05;
    double secondlevel = 0.10;
	double thirdlevel = 0.15;
	double  fourthlevel = 0.20;
	double coffee_per_pound = 12.99;
	double total_purchase_cost, pounds_wanted; 
	double firstlevelamt, secondlevelamt, thirdlevelamt, fourthlevelamt;
	//input and output
	cout << "How many pounds of coffee do you want to buy? " << endl;
	cin >> pounds_wanted;
	//calculations
	total_purchase_cost = coffee_per_pound * pounds_wanted;
	firstlevelamt = total_purchase_cost * firstlevel;
	secondlevelamt = total_purchase_cost * secondlevel;
	thirdlevelamt = total_purchase_cost * thirdlevel;
	fourthlevelamt = total_purchase_cost * fourthlevel;

	//if else statement for discount
	if ((pounds_wanted >= 0) && (pounds_wanted <= 4)) {
		cout << "Your final total is $ " << total_purchase_cost << endl;
	}
	else if ((pounds_wanted >= 5) && (pounds_wanted <= 9)) {
		cout << "Your final total purchase after quanity discount is $ " << total_purchase_cost - firstlevelamt << endl;
	}
	else if ((pounds_wanted >= 10) && (pounds_wanted <=19)) {
		cout << "Your final total purchase after qaunity discount is $ " << total_purchase_cost - secondlevelamt << endl;
	}
	else if (pounds_wanted >= 20) && (pounds_wanted <=19)) {
		cout << "Your final total purchase after quanity discount is $ " << total_purchase_cost - thirdlevelamt << endl;
	}
	else if (pounds_wanted >= 30) {
		cout << "Your final total purchase after quanity discount is $ " << total_purchase_cost - fourthlevel << endl;
	}

	return 0;
}
e]

[/code][/code]
Last edited on
Missing ( line 35

else if ( (pounds_wanted >= 20) && (pounds_wanted <=19) ) {
Last edited on
Thank you. My calculations are still wrong though
My calculations are still wrong though

The only way to handle this is to show us what you got and what you expected. Just saying ' it was wrong' isn't very good. :)

Well, you might like to simplify:

1. total_cost = (rate_per pound * pounds_wanted) * ( 100 - discount_rate )/100
2. step one doesn't have to be taken until the end
3. discount_rate depends on the pounds_wanted

So the pseudocode becomes

1
2
3
4
get the weight
get the discount_rate ( nested if statements are OK)
calculate total$
display total$


There are lots of ways of storing the discount rates. The simplest is to hard-code it into the if statement structure.



Last edited on
Hello cblack618,

A few things I found that should help.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;  // <--- Best not to use.
// The most recent post that is worth reading. http://www.cplusplus.com/forum/beginner/258335/

//main function
int main()
{
	constexpr double FIRSTLEVEL{ 0.05 };
	constexpr double SECONDLEVEL{ 0.10 };
	constexpr double THIRDLEVEL{ 0.15 };
	constexpr double FORTHLEVEL{ 0.20 };
	constexpr double COFFEE_PER_POUND{ 12.99 };

	//double firstlevel = 0.05;
	//double secondlevel = 0.10;
	//double thirdlevel = 0.15;
	//double  fourthlevel = 0.20;
	//double coffee_per_pound = 12.99;

	double total_purchase_cost{}, pounds_wanted{};
	double firstlevelamt{}, secondlevelamt{}, thirdlevelamt{}, fourthlevelamt{};

	//input and output
	std::cout << std::fixed << std::setprecision(2); // <--- Only needs done once.

	cout << "How many pounds of coffee do you want to buy? ";  // <--- Changed.
	cin >> pounds_wanted;

Lines 11- 15 would serve you better as these are variables that should not be changed.

Lines 23 and 24 it is best to initialize your variables especially ones for a total.

Line 29 if you remove the "endl" it will put the "std::cin" on the same line. Not really necessary, but I think it makes a better looking input.

The calculation section works. I do not believe there is anything wrong with it for the way you are using it.

Had you used constant variables with capital letters this would have been more noticeable
1
2
3
4
else if (pounds_wanted >= 30)
{
	cout << "Your final total purchase after quanity discount is $ " << total_purchase_cost - FORTHLEVEL/*fourthlevelamt*/ << endl;
}

I did not test each level yet, but what I have done appears to work except the lase "else if" statement.

Hope that helps,

Andy

Edit:

P.S. quanity is misspelled. "quantity" is correct.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

int main()
{
    double discount_rate = 0;
    double coffee_per_pound = 12.99;
    double pounds_wanted = 0;
    double total_purchase_cost = 0;
    
    cout << "How many pounds of coffee do you want to buy? ";
    cin >> pounds_wanted;
    
//    5-9 5%
//    10-19 10%
//    20-29 15%
//    30 or more 20%
    
    if(pounds_wanted < 5) {
        discount_rate = 0;
    }
    else if ((pounds_wanted >= 5) && (pounds_wanted <= 9)) {
        discount_rate = 5;
    }
    else if ((pounds_wanted >= 10) && (pounds_wanted <= 19)) {
        discount_rate = 10;
    }
    else if ((pounds_wanted >= 20) && (pounds_wanted <= 29)) {
        discount_rate = 15;
    }
    else{
        discount_rate = 20;
    }
    
    total_purchase_cost =
    (coffee_per_pound * pounds_wanted) * (100 - discount_rate)/100;
    
    cout << " Coffee/pound: " << coffee_per_pound << '\n';
    cout << "Pounds wanted: " << pounds_wanted << '\n';
    cout << "Discount rate: " << discount_rate << '\n';
    
    
    cout << "   Total cost: " << total_purchase_cost << '\n';
    
    return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <iomanip>

int main()
{
    double pounds_wanted = 0 ;
    std::cout << "How many pounds of coffee do you want to buy? " ;
    std::cin >> pounds_wanted;

    if( pounds_wanted > 0 ) // sanity check
    {
        const double price_per_pound = 12.99 ;
        const double gross_purchase_cost = pounds_wanted * price_per_pound ;

        // compute percent quantity discount
        double quantity_discount_pct = 5 ; // 5-9 5%
        if( pounds_wanted >= 10 ) quantity_discount_pct = 10 ; // 10-19 10%
        if( pounds_wanted >= 20 ) quantity_discount_pct = 15 ; // 20-29 15%
        if( pounds_wanted >= 30 ) quantity_discount_pct = 20 ; // 30 or more 20%

        // compute discount amount and nett price
        const double discount_amount = gross_purchase_cost * quantity_discount_pct / 100.0 ;
        const double nett_purchase_cost = gross_purchase_cost - discount_amount ;

        std::cout << '\n' << std::fixed << std::setprecision(2) // two digits after the decimal point
                  << "         quantity: " << pounds_wanted << " pounds\n"
                  << "       unit price: " << price_per_pound << " USD per pound\n"
                  << "quantity discount: " << quantity_discount_pct << "%\n\n"

                  << "gross cost: USD " << std::setw(10) << gross_purchase_cost << '\n'
                  << "  discount: USD " << std::setw(10) << discount_amount << "\n\n"
                  << " nett cost: USD " << std::setw(10) << nett_purchase_cost << '\n' ;
    }
}
Hello cblack618,

After further testing do you see a problem with this line?

else if ((pounds_wanted >= 20) && (pounds_wanted <= 19))

How can you be > 20 and < 19 at the same time?

Andy
It's coffee time while young Charles looks at his options :)

Has anybody checked our answers are consistent/correct?
Charles,

If I order 4.5 lbs of coffee your code gives no output. To avoid this sort of problem, it's best to use a ladder like this:
1
2
3
4
5
6
7
if (pounds_wanted < 0.0) {
    cout << "Purchase amount cannot be negative\n";
} else if (pounds_wanted < 5.0) {
    // full price
} else if (pounds_wanted < 10.0) {
    // 5% discount
} etc.

Alternatively, if coffee can only be purchased in whole pounds, then pounds_wanted should be an int or unsigned.

1
2
3
4
double firstlevel = 0.05;
double secondlevel = 0.10;
double thirdlevel = 0.15;
double  fourthlevel = 0.20;
Any time you see yourself writing variables like first, second, third, or foobar1, foobar2, foobar3, you should think of converting it to an array.

Also, as a general rule of thumb, data is smaller and faster than code. Putting these together, it makes sense to put the discount levels and amounts into a small table.

Also, keep in mind that the data that's convenient for the user is not necessarily the data that's convenient for the program. In this case, it's better for the program to store the price rate, rather than the discount rate:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
// #include <string>  // not needed
#include <iomanip>
using namespace std;

struct Price {
    double ceiling;		// The purchase amount must be less
				// than this to get this rate
    double rate;		// price per pound at this level
};

// The price rates, ordered by the ceiling amount. Note that this
// stores the actual price rate, which is (1 - discount)
Price rates[] = {
    { 5.0,	1.0 - 0.00 },	// No discount on amounts below 5
    { 10.0,	1.0 - 0.05 },	// 5% discount below 10
    { 20.0,	1.0 - 0.10 },
    { 30.0,	1.0 - 0.10 },
    { 1E99,	1.0 - 0.20 },	// 20% discount up to huge number
    { -1,	-1 },		// end of table
};

//main function
int main() {
    double total_purchase_cost, pounds_wanted;
    double retail_rate = 12.99;	// full retail rate

    cout << "How many pounds of coffee do you want to buy? " << endl;
    cin >> pounds_wanted;
    //calculations

    // Go through the rates table. When you find the rate for this
    // quantity, compute the total code and break out of the loop.
    // This assumes that the rates are in increasing order of quantity
    // and that the table ends with a negative rate
    for (unsigned i = 0; rates[i].rate >= 0; ++i) {
	if (pounds_wanted < rates[i].ceiling) {
	    total_purchase_cost = pounds_wanted * rates[i].rate * retail_rate;
	    break;
	}
    }
    std::cout << std::fixed << std::setprecision(2);
    cout << "Your final total is $ " << total_purchase_cost << endl;
    return 0;
}

Hello thank you for your help
Topic archived. No new replies allowed.