Trouble with nested while loop

Sorry if this is bad formatting first time on the website. But I'm trying to make a sample bookstore for an assignment, but for some reason on the second loop my totals do not add up correctly. I have been fiddling with it for the best part of 2 hours and my output always seems to be too high. for instance with a sample output of.


Customer 1
Enter the cost of book:
5
Enter "h" for HARD COPY, any other key for PAPER_COPY:
h
Enter "d" if customer receives a discount, any other key if they don't:
d
Enter "c" to check out another book, any other key to total Customer 1:
c

Customer 1
Enter the cost of book:
9
Enter "h" for HARD COPY, any other key for PAPER_COPY:
j
Enter "d" if customer receives a discount, any other key if they don't:
j
Enter "c" to check out another book, any other key to total Customer 1:
j

Customer 1:
Total books checked out: 2
Total before tax and discount: 21.00
Total Sale for Customer 1: 21.20

Welcome to Book Store.
Enter "c" to checkout a new customer, or "q" to close batch and print a daily report.
c

Customer 2
Enter the cost of book:
10
Enter "h" for HARD COPY, any other key for PAPER_COPY:
d
Enter "d" if customer receives a discount, any other key if they don't:
d
Enter "c" to check out another book, any other key to total Customer 2:
j

Customer 2:
Total books checked out: 1
Total before tax and discount: 12.00
Total Sale for Customer 2: 11.45

Welcome to Book Store.
Enter "c" to checkout a new customer, or "q" to close batch and print a daily report.
q
Average customer sale: 15.40
Max customer sale: 21.00
Total customer sale (without tax): 30.80
Total daily sales: 32.65
Tax: 1.85
Discount: 2.20


but for the last lines my code gives

Average customer sale: 25.40
Max customer sale: 21.00
Total customer sale (without tax): 50.80
Total daily sales: 32.65
Tax: 2.39
Discount: 19.80

with the rest being correct. I think it has something to do with my calculation of the discount as that is the biggest outlier but still not sure.


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
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

int main()
{
	cout << fixed << showpoint << setprecision(2);
	char customerOrquit, bookCover, discount;														//setting var
	int customer, books; 
	float bookCost, bookCostd, bookCostTotal, bookCostTotal2,bookCostTotal3, salesTotal ,salesTotal2 ,maxSale ,averageSale, discountTotal, tax,taxtotal;

	tax = 0;
	taxtotal = 0;
	customer = 0;
	discountTotal = 0;
	salesTotal = 0;																					//initializing var
	salesTotal2 = 0;
	maxSale = 0;
	averageSale = 0;
	bookCost = 0;
	bookCostd = 0;
	bookCostTotal = 0;
	bookCostTotal2 = 0;
	bookCostTotal3 = 0;
	customerOrquit = 'c';
	discount = 'd';

	while (customerOrquit != 'q' && customerOrquit != 'Q')															//outer while
	{
		cout << "Welcome to Book Store." << endl << "Enter \"c\" to checkout a new customer, or \"q\" to close batch and print a daily report." << endl;
		cin >> customerOrquit;

		if (customerOrquit == 'C' || customerOrquit == 'c')															//if close or not
		{
			customer++;
			books = 0;
			bookCostTotal = 0;
			bookCostTotal2 = 0;
			bookCost = 0;
			bookCostd = 0;

			while (customerOrquit == 'c' || customerOrquit == 'C')
			{
				cout << "Customer " << customer << endl << "Enter the cost of book: " << endl;
				cin >> bookCost;

				books++;

				cout << "Enter \"h\" for HARD COPY, any other key for PAPER_COPY: " << endl;
				cin >> bookCover;

				if (bookCover == 'h' || bookCover == 'H')
				{
					bookCost = bookCost + 5;

				}
				else
				{
					bookCost = bookCost + 2;
				}

				cout << "Enter \"d\" if customer receives a discount, any other key if they don't: " << endl;
				cin >> discount;



				if (discount == 'd' || discount == 'D')
				{
					bookCostd = bookCost * .9;
					bookCostTotal = bookCostd + bookCostTotal;
					discountTotal = bookCostd + discountTotal;
					bookCostTotal3 = bookCostd + bookCostTotal3;
				}
				else
				{
					bookCostTotal3 = bookCost + bookCostTotal3;
					bookCostTotal = bookCost + bookCostTotal;
				}

				bookCostTotal2 = bookCostTotal2 + bookCost;

				cout << "Enter \"c\" to check out another book, any other key to total Customer 1:" << endl;
				cin >> customerOrquit;

				tax = bookCostTotal * .06;
				taxtotal = tax + taxtotal;
				if (customerOrquit != 'c' && customerOrquit != 'C')
				{
					cout << "Customer " << customer << endl << "Total books checked out: " << books
						<< endl << "Total before tax and discount: " << bookCostTotal2 << endl << "Total Sale for Customer 1: " << tax + bookCostTotal << endl;
					bookCostTotal = bookCostTotal * 1.06;
					salesTotal2 = bookCostTotal + salesTotal2;
					salesTotal = bookCostTotal3 + salesTotal;
				}
				if (maxSale < bookCostTotal2)
				{
					maxSale = bookCostTotal2;
				}
			}
		}
		else
		{
			averageSale = salesTotal / customer;

			cout << "Average customer sale : " << averageSale << endl;
			cout << "Max customer sale: " << maxSale << endl;
			cout << "Total customer sale (without tax): " << salesTotal << endl;
			cout << "Total daily sales: " << salesTotal2 << endl;
			cout << "Tax: " << taxtotal << endl;
			cout << "Discount: " << discountTotal << endl;
		}
	}


	cin.get();
	cin.get();

	return 0;
}


Thank you for any possible help
Last edited on
Fixed part of it by initializing a discountAmount float variable. and using that for the discount total equation.
1
2
3
4
5
					bookCostd = bookCost * .9;
					discountAmount = bookCost * .1;
					bookCostTotal = bookCostd + bookCostTotal;
					discountTotal = discountAmount + discountTotal;
					bookCostTotal3 = bookCostd + bookCostTotal3;


and for some reason that helped the total output no idea why.

Average customer sale: 19.90
Max customer sale: 21.00
Total customer sale (without tax): 39.80
Total daily sales: 32.65
Tax: 2.39
Discount: 2.20

Again no idea why it helped but happy it did haha.
Ok changed a bit again this time moving the salesTotal = bookCostTotal3 + salesTotal; around a bit and that fixed my output mostly now just gotta fix the tax output.

Ok just fixed that as well by moving the taxtotal = tax + taxtotal; into the if statement that had the rest of the totals in it. Not sure why any of this fixed what happened but it is technically fixed haha!
Last edited on
Topic archived. No new replies allowed.