Bakery Billing Program

So, the point of this program is to make simple billing program for a bakery. The program starts by having the cashier enter in the prices for single muffin, a half-dozen, and a dozen muffins. Then, the cashier enters in the amount that the customer wants. Based on the amount entered, the program will return the best offer. (For example, if the customer wants 11 muffins, but the price of 11 individual muffins is greater than the price of a dozen muffins, the program will recommend the customer by a dozen muffins). I'm having trouble getting my program to do the calculations correctly. I think my algorithm is correct, but it's not displaying properly. Any help would be greatly appreciated!

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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137

#include <iostream>
#include <iomanip>
#include <cstring>
using namespace std;

class Bakery
{
    
public:
	void putPrice(double, double, double);
        void putAmount(int x) { amount = x; }
	double calcPrice();
	void displayPrice();
        void newOrder();
	
private:
	double price1;
	double price6;
	double price12;
	double totalPrice;
        double trialPrice;
        double trialPrice2;
	int amount;
    
};



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

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

void Bakery::displayPrice()
{
	cout << "Amount due for this customer is: " << totalPrice << endl << endl;
}

void Bakery::newOrder()
{
    bool order = false;
    
    while (order != true)
    {
        char input;
        
        cout << "Would you like to take another order? ";
        cin >> input;
        
        if (input == 'Y' || input == 'y')
        {
            order = true;
            cout << endl << endl << endl;
        }
        return;
    }
}


int main()
{
    Bakery myMuffinShop;
    double single, half, dozen;
    int amount;
    
    cout << "Enter the price of a single muffin, half-dozen, and dozen" <<
    endl;
    cin >> single >> half >> dozen;
    
    if (!cin.eof() && cin.good())
    {
        myMuffinShop.putPrice(single,half,dozen);
        
        do
        {
            cout << "\nHow many muffins does the next customer want to order? ";
            cin >> amount;
            
            myMuffinShop.putAmount(amount);
            myMuffinShop.calcPrice();
            myMuffinShop.displayPrice();
            myMuffinShop.newOrder();
        }
        
        while (!cin.eof() && cin.good() && amount > 0);
    }
    
    else
    {
        cout << "\nInvalid prices entered." << endl;
        cout << "Program terminating..." << endl << endl;
        return 0;
    }
}


Line 40: amount is an uninitialized local variable. You then proceed to use this uninitialized variable in lines 42-47. Did you intend to use the member variable amount at line 24? The local variable hides the member variable with the same name.

What is the point of newOrder() ? As far as I can see all it does is output 3 line feeds if the user says yes. If the user says N, the loop at lines 114-125 continues.
Last edited on
Yes, I meant to use the member variable amount at line 24. Made the change, but still not getting correct total.

The point of newOrder() is to keep the program running, so that I can take additional orders.
Last edited on
The point of newOrder() is to keep the program running, so that I can take additional orders

How does it do that?

As I pointed out, it asks the user if they want to continue, The result of that question is ignored and has NO bearing on the flow of main.
Yeh, I mean, I clearly have to fix that, but I'm trying to do one thing at a time. Haha. Even when I take all the loops out, it still doesn't give me the correct number.
I added a breakpoint at the call calcTot and found that the algorithm is wrong for some reason. all the variables load correctly, but the computation of totalPrice is incorrect and I'm not sure why. Even when I try it this way:

1
2
3
4

    (trialPrice >= calcPrice12 ? totalPrice += calcPrice12 : trialPrice += (amount%12)/6 * calcPrice6 );
    (((amount%6) * calcPrice1) >= trialPrice ? totalPrice += calcPrice6 : totalPrice = (amount * calcPrice1));


it still gives me the same wrong answer. any suggestion?
I see no calcTot function in the code you posted before.
Please post your current code.
I meant calcPrice(), but here it is:

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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147


#include <iostream>
#include <iomanip>
#include <cstring>
using namespace std;

class Bakery
{
    
public:
    Bakery() {}
    Bakery(double x, double y, double z ) { price1 = x; price6 = y; price12 = z; }
	void putPrice(double, double, double);
    void putAmount(int w) { amount = w; }
	void calcPrice();
	void displayPrice();
   // void newOrder();
	
private:
	double price1;
	double price6;
	double price12;
	double totalPrice;
    double trialPrice;
    double trialPrice2;
	int amount;
    
};



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

void Bakery::calcPrice()
{
	double calcPrice1, calcPrice6, calcPrice12 = 0;
    
	calcPrice12 = (amount/12) * price12;
	calcPrice6 = (amount/6) * price6;
	calcPrice1 = amount * price1;
	
	totalPrice = calcPrice12;
	trialPrice = (amount%12) * calcPrice1;
    
    (trialPrice >= calcPrice12 ? totalPrice += calcPrice12 : trialPrice += (amount%12)/6 * calcPrice6 );
    (((amount%6) * calcPrice1) >= trialPrice ? totalPrice += calcPrice6 : totalPrice = (amount * calcPrice1));
    
    
/*	if(trialPrice >= calcPrice12)
    {
        totalPrice += calcPrice12;
    }
    
	if (trialPrice < calcPrice12)
    {
        trialPrice += (amount%12)/6 * calcPrice6;
        trialPrice2 += (amount%6) * calcPrice1;
        
        if (trialPrice2 >= calcPrice6)
        {
            totalPrice += calcPrice6;
        }
        
        else
        {
            totalPrice += amount * calcPrice1;
        }
    } */
}

void Bakery::displayPrice()
{
	cout << "Amount due for this customer is: " << totalPrice << endl << endl;
    
}

/* void Bakery::newOrder()
{
    bool order = false;
    
    while (order != true)
    {
        char input;
        
        cout << "Would you like to take another order? ";
        cin >> input;
        
        if (input == 'Y' || input == 'y')
        {
            order = true;
            cout << endl << endl << endl;
            return;
        }

        else if (input == 'N' || input == 'n')
        {
            return;
        }
    }
} */


int main()
{
    Bakery myMuffinShop;
    double single, half, dozen;
    int amount;
    
    cout << "Enter the price of a single muffin, half-dozen, and dozen" <<
    endl;
    cin >> single >> half >> dozen;
    
    if (!cin.eof() && cin.good())
    {
        myMuffinShop.putPrice(single,half,dozen);
        
      //  do
     //   {
            cout << "\nHow many muffins does the customer want to order? ";
            cin >> amount;
        
            if (!cin.eof() && cin.good() && amount > 0)
            {
                myMuffinShop.putAmount(amount);
                myMuffinShop.calcPrice();
                myMuffinShop.displayPrice();
                // myMuffinShop.newOrder();
            }
     //   }
        
    }
    
    else
    {
        cout << "\nInvalid prices entered." << endl;
        cout << "Program terminating..." << endl << endl;
        return 0;
    }
}

Topic archived. No new replies allowed.