Question on Price Calculation Program

I've just learned about classes and objects and I'm having trouble trying to calculate an order for a price of cookies, which is the goal of the program.

The program is supposed to calculate the price of an order of cookies offering the best deal possible.
The program starts off with the user entering the price of cookies for the day.
The price is entered for: single cookie, half-dozen cookies and a dozen. After prices are entered the program for an order which it takes as input from the customer. After taking the order its supposed to calculate the price of the order and offer the best deal. This is where my problem is because there are constraints for the prices and what can be accepted as order inputs. Constraints are listed below:
1. All prices have to be greater than 0, (I'm ok with this)
2.The price of a half-dozen, must be less than the price of six individual cookies.
3. The price of a dozen must be less than the price of two half-dozen.
This sets up the possibility that the price of 5 or fewer could be greater than the price of a half-dozen, and price of 11 or fewer could be greater than price of a dozen. If this price situation does happen program is supposed to advise the customer of the better deal.
4. So the program can't accept the price of a halfdozen thats more than price of a dozen, or price of 6 single cookies thats more than a half dozen.

I'm not sure if I need more set or get methods or if its just my calculations.
I keep getting 0 for my totalPrice each run. I'm looking to understand how and what I've done wrong, so I can learn from this. Thank you all for you 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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <iostream>
#include <iomanip>
#include <cstring>
using namespace std;

class CookieStore{

public: 
	void setPrice(double, double, double);
	double calcPrice(int);
	void displayPrice();
	
	
	
private:
	double price1;
	double price6;
	double price12;
	double totalPrice;
	int amount;

};



void CookieStore::setPrice(double a, double b, double c)
{
	price1 = a;
	price6 = b;
	price12 = c;
}

double CookieStore::calcPrice(int amount)
{	
	double calcPrice1, calcPrice6, calcPrice12 = 0;
	
	calcPrice12 = (amount/12) * price12;
	calcPrice6 = (amount/6) * price6;
	calcPrice1 = amount * price1;
	
	totalPrice = calcPrice1;
	
	if((price1* 6) > calcPrice6)
	totalPrice = calcPrice6;
	else if((price6 * 2) > calcPrice12)
	
	return totalPrice;
}

void CookieStore::displayPrice()
{
	cout << "Amount due is: " << totalPrice;

}


	int main(){
		CookieStore myCookieStore;
		double single, half, dozen;
		int order;

		cout << " Enter the price of a single cookie, half-dozen, and dozen" <<
		endl;
		cin >> single >> half >> dozen;
		if(!cin.eof() && cin.good() && order > 0){
			myCookieStore.setPrice(single,half,dozen);
		}
		else{
			cout << "Invalid Prices entered" << endl;
		}


	
		cout << "How many cookies does the customer want to order?" << endl;
		cin >> order;

		while(!cin.eof() && cin.good() && order > 0){
		myCookieStore.calcPrice(order);
		myCookieStore.displayPrice();
		order = 0;
		}

	return EXIT_SUCCESS;
}
	
 
Line 65.
if(!cin.eof() && cin.good() && order > 0){

Order has been assigned a value yet.

Line 45: Doesn't do anything. Put
totalPrice = calcPrice12;

after the else if.

Also your function calcPrice() returns a double, but you don't use the return for anything so consider making it void.
Last edited on
Hi

your program does not perform as requested.
The function setPrice is fine. Except for the fact that it does not test the two constraints on prices.

So, you have to implement this. I suggest you implement the function setPrice as follows:
in Pseudocode
1
2
3
4
5
bool setprice(double, double, double); 
if 6 times price of 1 cookie is more then price for half a dozen then return 0
if price for two half dozens is more then price for a dozen then return 0
if not then set the prices and 
return 1


Now, you should make your input of prices a while loop that runs until the function setprices returns a true.

After you managed that, you have to ask the customer for the amount of cookies he wants.

then you have to check for best offer:
Following the constraints, you have two cases two check: Order of less than 6 or of 6 to 11 cookies more than a multiple of dozens.
Mathematically, the amount of full dozens is of no interest.
in Psuedocode:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
declare the int dozens // will hold the number of full dozens of cookies
dozens = order / 12
order = order%12  // order will keep the number of cookies that is smaller then 12

if order < 6 then check 
      if price1 * order > price6 then totalprice = price6 and order = 6
      else totalprice = price1 * order

if order > 5 && < 12 then check
     if price6 + price1 * (order-6) > price 12 then totalprice = price12 and order = 12
     else totalprice = price 6 + price1 * (order-6)

totalprice = totalprice + dozens * price12
order = order + 12*dozens

put out the suggestion to buy the amount of order for the price of totalprice


int main


Last edited on
Topic archived. No new replies allowed.