Checking for errors.

I have errors in my code that I can't figure out. Any help would be appreciated.

There's something wrong with my calculateTax function. I have my taxes defined at a certain value, but my final tax near the end of my code always comes out to $0.00.

And I also need help with my displayCatalog function. It's part of a framework that my professor gave to my class for this project, but it constantly loops after I input the item choice and it won't add the items to my cart.

I also need help with my loadInventory function. I have the file created and it opens, but I don't know how to get the function to read each line to save it to an item array. And I also need to make it so it reads each item in the file and update the catalog size property. The format of the files should be
ItemId ItemName ranking height width length price weight inventoryCount.
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#include <iostream>
#include <string>
#include <ctime>
#include <fstream>
using namespace std;

const int MAX_CATALOG_SIZE = 20;

struct Cart 
{
	int itemId;
	string itemName;
	int volume;
	double price, weight;
	int qtyPurchased;
};
Cart shoppingCart[20];

class Item {
private:
	int itemId, itemHeight, itemWidth, itemLength, inventoryCount;
	double itemPrice, itemWeight;
	string itemName, itemRanking;
public:
		Item()
		{
			itemId = 0;
			itemName = " ";
			itemPrice = 0.00;
			itemHeight = 0;
			itemWidth = 0;
			itemLength = 0;
			itemWeight = 0.00;
			itemRanking = " ";
			inventoryCount = 0;
		}

		void setItem(int id, int h, int w, int l, int count, double p, double wt, string n, string r)
		{
			itemId = id;
			itemHeight = h;
			itemWidth = w;
			itemLength = l;
			inventoryCount = count;
			itemPrice = p;
			itemWeight = wt;
			itemName = n;
			itemRanking = r;
		}

		float getVolume()
		{
			float vol = itemHeight*itemWidth*itemLength;
			return vol;
		}

		int getWeight()
		{
			return itemWeight;
		}

		string getName()
		{
			return itemName;
		}

		int getId()
		{
			return itemId;
		}

		float getPrice()
		{
			return itemPrice;
		}

		string getRank()
		{
			return itemRanking;
		}

		int getInventory()
		{
			return inventoryCount;
		}

		void setInventory(int value)
		{
			cout << "Input the current inventory amount.";
			cin >> value;
			value += inventoryCount;

				if (value < 0)
					value--;
				if (inventoryCount < 0)
				{
					cout << "Inventory amount = 0";
					inventoryCount = 0;
				}
		}
};

/*
*	This class definition represents all of the available items for sale. It contains two private properties
*	itemList is an array that represents each each for sale. It uses the item class.
*	size = this represents the actual number of items in the catalog based upon what is read into from the data
*	file. Size needs to be less than MAX_CATALOG_SIZE. 
*/

class Catalog {
private:
	Item itemList[MAX_CATALOG_SIZE];
	int size;

public:
	Catalog() 
	{
		size = 0;
	}

	/*
	*  This method prints out the contents of each item in the catalog. It can be used for debugging purposes
	*  and does not need to be invoked as part of the final version of the program.
	*
	*	Do NOT modify this method. 
	*/

	void printCatalog() 
	{
		for (int i = 0; i < size; i++) {
			cout << "catalog[" << i << "] ID = " <<  itemList[i].getId() 
				<< " name =" << itemList[i].getName() 
				<< " price =" << itemList[i].getPrice() 
				<< " volume = " << itemList[i].getVolume()
				<< " weight = " << itemList[i].getWeight()
				<< " count = " << itemList[i].getInventory()
				<< " ranking = " << itemList[i].getRank()
				<< endl;
		}
	}

	/*
	*	This method will display the catalog to the user. It validates the user input to make sure it is an integer.
	*	It then returns the item ID. If it finds the item, it will update the cart item structure. 
	*	Note - cart item structure is an OUTPUT parameter and is passed by reference.
	*
	*	Do NOT modify this method. It should be called from maintain cart.
	*/

	int displayCatalog(Cart&cartItem) {
		bool validChoice = false;
		int itemChoice;
		do {
			int size;
			cout << "Please choose an item from the following list. Enter the item number: " << endl;
			cout << "1. Ton-Tongue Toffee" << endl;
			cout << "2. Self-Writing Quill" << endl;
			cout << "3. Wildfire Whiz-bangs" << endl;
			cout << "4. Felix Felicis" << endl;
			cout << "5. Dragon Fire" << endl;
			cin >> size;
			for (int i = 0; i < size; i++) {
				cout << "Item (" << itemList[i].getId() << ") name = " << itemList[i].getName() 
					<< " Remaining quantity = " << itemList[i].getInventory() << endl;
			}
			cout << "Item Selected: ";
			cin >> itemChoice;
			if (cin.fail() ) 
			{
				cin.clear();
				cin.ignore(5);
			} 
			else {
				for (int i = 0; i < size; i++)
					if (itemList[i].getId() == itemChoice)
						if (itemList[i].getInventory() <= 0)
							cout << "That item is no longer available." << endl;
						else {
							cartItem.itemId = itemList[i].getId();
							cartItem.itemName = itemList[i].getName();
							cartItem.volume = itemList[i].getVolume();
							cartItem.weight = itemList[i].getWeight();
							cartItem.price = itemList[i].getPrice();
							validChoice = true;
						}
			}
		} while (!validChoice);
		return itemChoice;
	}

	/*
	* The setCatalogItem function may not be needed by your application. It will assign specific item values to the
	* next empty spot in the catalog. It uses the size property as the last item in the catalog. It then increments
	* the size value. It assumes that setItem is a valid method in the item class. 
	*/

	void setCatalogItem(int id,  int h, int w, int l, int count, double p, double wt, string n, string r) 
	{
		itemList[size].setItem(id, h, w, l, count, p, wt, n, r);
		size++;
	}

	/*
	*	This function updates the existing quantity for a specific item. It will loop through all items in
	*	the catalog. If it can't find the item, it returns false. 
	*
	*	Do NOT modify this method. It should be called from maintain cart or from return item.
	*/

	bool setInventoryCount(int id, int qtyChange) {
		bool found = false;
		for (int i = 0; !found && i < size; i++)
			if (id == itemList[i].getId()) {
				itemList[i].setInventory(qtyChange);
				found = true;
			}
		return found;
	}

	/*
	*	This function returns the current quantity for a specific item. It will loop through all items in
	*	the catalog. If it can't find the item, it returns -1 indicating an invalid amount. 
	*
	*	Do NOT modify this method. It should be called from maintain cart or from return item.
	*/
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
int getInventoryCount(int id) {
		int count = -1;
		for (int i = 0; count == -1 && i < size; i++)
			if (id == itemList[i].getId()) {
				count = itemList[i].getInventory();
			}
		return count;
	}

	/*
	*	This method will open up a file based on the input fileName parameter. This file contains all of the 
	*	items. It will read in each item from the file and save it to the catalog array. You can use the setItem()
	*	method in the item class to update all of the values. 
	*	For each line read in from the file, it needs to increment the size property.
	*/

	void loadInventory(string fileName, Item info, int id, int h, int w, int l, int count, double p, double wt, string n, string r)
	{
		ofstream outStream;
		ifstream inStream;
		string line;

		inStream.open("inventory.txt");
		if (inStream.fail()) {
			cout << "Please enter the file name: " << endl;
			cin >> fileName;
		}
	}

};

//	OTHER STORE FUNCTIONS GO HERE - MAINTAINCART, CALCULATETAX, ETC.

int maintainCart(Cart&cartItem, Catalog catalogItem, int id, int inventoryCount, Item info, int value)
{
	cout << "How many would you like to buy?" << endl;
	cin >> cartItem.qtyPurchased;
	
	catalogItem.printCatalog();

	while (cartItem.qtyPurchased < 1)
	{
		cout << "Invalid amount. You must choose one item.";
		cin >> cartItem.qtyPurchased;
	}

	catalogItem.getInventoryCount(id);
	info.setInventory(value);
	inventoryCount++;

	return cartItem.qtyPurchased;
	cout << endl;

	char choice;
	cout << "Would you like to continue shopping? (Y/N)" << endl;
	cin >> choice;
	cout << endl;
}

string createCustomer (string&name, string&address)
{
	cout << "Time to create your customer account." << endl << endl;
	cout << "Please enter your name: ";
	cin >> name;
	cin.clear();
	cin.ignore();
	cout << "Please enter your address: ";
	getline(cin, address);
	cout << "Hello " << name << ", your address is " << address << "." << endl;
	cout << "Your customer account has been created!" << endl;
	cout << endl;
	return name;
	return address;
}

int displayMenu()
{
	int num;
	cout << "Please choose a menu option: " << endl;
	cout << "1. Create Customer Account" << endl;
	cout << "2. Shop for Items" << endl;
	cout << "3. Proceed to Checkout" << endl;
	cout << "4. Exit Store" << endl;
	cout << "5. Debug" << endl;
	cout << "6. Empty Cart" << endl;
	cin >> num;
	cout << endl;
	return num;
}

void itemRank (int&num)
{
	if (num == 1)
		cout << "RATING: ***" << endl;
	else if (num == 2)
		cout << "RATING: ****" << endl;
	else if (num == 3)
		cout << "RATING: **" << endl;
	else if (num == 4)
		cout << "RATING: *" << endl;
	else 
		cout << "RATING: *****" << endl;
}

void catalogChoice(float cost, float price1[], int&num, int id, int count, int value, string&cart, char&choice, Cart&cartItem, Catalog catalogItem, Item info)
{
	catalogItem.displayCatalog(cartItem);
	string item [5] = {"Ton-Tongue Toffee", "Self-Writing Quill", "Wildfire Whizbangs", "Felix Felicis", "Dragon Fire"};
	cout << "You have picked " << item[num] << "." << endl;
	itemRank(num);
	cost = price1[num];
	cout << "Your current total is $" << cost << "." << endl;
	cart += item[num];
	maintainCart(cartItem, catalogItem, id, count, info, value);

	//prompt user about shopping
	cout << "Would you like to continue shopping? (Y or N)";
	cin >> choice;
	cout << endl;
}

void calculateShipping(float cost, float height, float weight, float length, float width,
					   float rate, float vol, float ship, float&shipCost, Cart cartItem)
{
	//package dimensions for cost
	cout << "Package's weight?" << endl;
	cin >> weight;
	cout << endl;
	cout << endl;
	vol = height*width*length; 
	ship = vol/1000.0;
	cout.setf(ios::fixed);
	cout.precision(2);
	shipCost = (rate*weight) + ship;
	cout.setf(ios::fixed);
	cout.precision(2);
	cout << "Total shipping cost: $" << shipCost << endl;
}

float calculateTax(float&tax, float&taxCost, float&cityTax, float&cost, int&quantity, float&ship, float&height, float&weight, 
				   float&length, float&width, float&rate, float&vol, int&day, int&shipDay)
{
	int cityChoice;

	//ask user to choose a store in certain cities
	cout << "We offer business in: " << endl;
	do{
		cout << "1. Los Angeles" << endl;
		cout << "2. San Diego" << endl;
		cout << "3. Beverly Hills" << endl;
		cout << "Choose a city: " << endl;
		cin >> cityChoice;
		if (cityChoice < 1 || cityChoice > 3)
			cout << "Our business isn't located there!" << endl;
	}while (cityChoice < 1 || cityChoice > 3);
	cout << endl;

	switch (cityChoice)
	{
	case 1: 
		cityTax = 9.00;
		break;
	case 2:
		cityTax = 9.00;
		break;
	case 3:
		cityTax = 8.00;
		break;
	}

	taxCost = (cityTax/100.0) * cost;
	return taxCost;
}

void calculateShipDay(int&day, int&shipDay, int&num, float&cost, float&height, float&weight, float&length, 
					  float&width, float&rate, float&vol, float&ship, float&tax, float&shipCost, Cart&cartItem)
{
	//user selected method
	cout << "What day of the month is it?" << endl;
	cin >> shipDay;
	cin.clear();
	cin.ignore();

	//ask user about shipping method
	cout << "Which shipping method do you want to use?" << endl;
	cout << "1. USPS (10 days)" << endl;
	cout << "2. UPS (7 days)" << endl;
	cout << "3. FedEx (4 days)" << endl;
	cin >> num;
	cin.clear();
	cin.ignore();
	cout << endl;

	switch (num)
	{
		case 1:
			day = 10;
			rate = 1.55;
			calculateShipping(cost, height, weight, length, width, rate, vol, ship, shipCost, cartItem);
			tax = (tax*cost)/100.0;

			//estimate shipping date
			if (shipDay > 0 && shipDay < 19)
			{
				cout << "Your product will arrive on day " << (abs)(day + shipDay) << " of this month." << endl;
				cout << endl;
			}
			else if (shipDay >= 19 && shipDay <=30)
			{
				cout << "Your product will arrive on day " << (abs)(30 - (day + shipDay)) << " of next month." << endl;
				cout << endl;
			}
				break;

		case 2:
			day = 7;
			rate = 1.65;
			calculateShipping(cost, height, weight, length, width, rate, vol, ship, shipCost, cartItem);
			tax = (tax*cost)/100.0;

			//estimate shipping date
			if (shipDay > 0 && shipDay < 27)
			{
				cout << "Your product will arrive on day " << (abs)(day + shipDay) << " of this month." << endl;
				cout << endl;
			}
			else if (shipDay >= 27 && shipDay <= 30)
			{
				cout << "Your product will arrive on day " << (abs)(30 - (day + shipDay)) << " of next month." << endl;
				cout << endl;
			}
				break;
		case 3:
			day = 4;
			rate = 1.72;
			calculateShipping(cost, height, weight, length, width, rate, vol, ship, shipCost, cartItem);
			tax = (tax*cost)/100.0;

			//estimate shipping date
			if (shipDay > 0 && shipDay < 25)
			{
				cout << "Your product will arrive on day " << (abs)(day + shipDay) << " of this month." << endl;
				cout << endl;
			}
			else if (shipDay >= 25 && shipDay <= 30)
			{
				cout << "Your product will arrive on day " << (abs)(30 - (day + shipDay)) << " of next month." << endl;
				cout << endl;
			}
				break;
		default:
			day = 10;
			rate = 1.55;
			calculateShipping(cost, height, length, weight, width, rate, vol, ship, shipCost, cartItem);
			tax = (tax*cost)/100.0;

			//estimate shipping date
			if (shipDay > 0 && shipDay < 19)
			{
				cout << "Your product will arrive on day " << (abs)(day + shipDay) << " of this month." << endl;
				cout << endl;
			}
			else if (shipDay >= 19 && shipDay <=30)
			{
				cout << "Your product will arrive on day " << (abs)(30 - (day + shipDay)) << " of next month." << endl;
				cout << endl;
			}
				break;
	}
}

float calculateSubtotal(float&subtotal, float&shipCost, float cost, int quantity)
{
	subtotal = (shipCost + cost)*quantity;
	return subtotal;
}

float calculateTotal(float&tax, float&subtotal, float&total)
{
	total = subtotal + tax;
	return total;
}

void testTax()
{
	float cityTax = 6.4;
	float cost = 7.00;
	float final = (cityTax*cost)/100.0;
	cout.setf(ios::fixed);
	cout.precision(2);
	cout.setf(ios::fixed);
	cout.precision(2);
	cout << "Test tax = $" << final << endl;
}
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
void testTotal()
{
	float tax = 0.45;
	float subtotal = 1145.60;
	float shipCost = 13.00;
	float final = tax + subtotal + shipCost;
	cout << "Test total = $" << final << endl;
}

void testShipping()
{
	float cost = 11.50;
	float height = 9;
	float weight = 5;
	float width = 5;
	float length = 3;
	float rate = 5.4;
	float vol;
	float ship;
	float shipCost;
	vol = height*width*length;
	shipCost = vol/1000.0;
	cout.setf(ios::fixed);
	cout.precision(2);
	float final = cost;
	cout << "Test shipping = $" << shipCost << endl;
	cout << endl;
}

void testSubtotal()
{
	int quantity = 4;
	float cost = 6.25;
	float subtotal = 90.14;
	subtotal = subtotal + (cost*quantity);
	float final = subtotal;
	cout << "Test subtotal = $" << final << endl;
}

void emptyCart(int&num, int value, int id, int qtyChange, int count, char&choice, string&cart, Item info, Catalog catalogItem, Cart cartItem)
{
	cout << "Would you like to empty your cart? (Y or N)" << endl;
	cin >> choice;
	if (choice == 'y' || choice == 'Y')
	{
	cout << "Choose between: " << endl;
	cout << "1. A specific item" << endl;
	cout << "OR" << endl;
	cout << "2. The whole cart" << endl;
	cin >> num;
	cin.clear();
	cin.ignore();

		if(num == 1)
		{
			
			string item [5] = {"Ton-Tongue Toffee", "Self-Writing Quill", "Wildfire Whizbangs", "Felix Felicis", "Dragon Fire"};
			cout << "What item would you like to remove?" << endl;
			cin >> num;
			cin.clear();
			cin.ignore();
			cout << "You want to remove " << item[num] << "." << endl;
			count = 0;
			catalogItem.setInventoryCount(id, qtyChange);
		}
	
		else if(num == 2)
		{
			info.setInventory(value);
		}
	}

	else displayMenu();
}

void saveInvoice(string&cart, float&shipCost, float&taxCost, float&subtotal, string&name)
{
	ofstream outStream;
	outStream.open("invoice.txt");

	cout << "CUSTOMER: " << name << endl << endl;
	cout << "ITEMS: " << cart << endl << endl;
	cout << "SUBTOTAL: $" << subtotal << endl << endl;
	cout << "TAX: $" << taxCost << endl << endl;
	cout << "SHIPPING: $" << shipCost << endl << endl;
	cout << "GRAND TOTAL: $" << subtotal + taxCost + shipCost << endl;
	
	outStream.close();
}

/*
*	Modify your main program as needed to use the catalog definitions above.
*/

int main() {

	srand((unsigned)time(NULL));

	//define and initialize variables needed

	int i;
	int num = 0;
	int cityChoice;
	int day;
	int shipDay;
	char choice = '\0';
	string name;
	string address;
	string cart = " " ;
	string fileName = " ";
	float price1[5] = {0, 0, 0, 0, 0};
	price1[0] = rand()%5+10;
	price1[1] = rand()%5+10;
	price1[2] = rand()%10+10;
	price1[3] = rand()%10+20;
	price1[4] = rand()%20+40;
	float price = 0;
	int quantity = 0;
	float cost = 0.0;
	float subtotal = 0;
	float total = 0.0;
	float cityTax = 0.0;
	float tax = 0.0;
	float taxCost = 0.0;
	float ship = 0.0;
	float vol = 0.0;
	float height = 0.0;
	float weight = 0.0;
	float length = 0.0;
	float width = 0.0;
	float rate = 0.0;
	float shipCost = 0.0;
	int id = 0, h = 0, w = 0, l = 0, count = 0, value = 0, qtyChange = 0;
	double p = 0.00, wt = 0.00;
	string n = " ", r = " ";
	Cart cartItem;
	Catalog catalogItem;
	Item info;


	catalogItem.loadInventory(fileName, info, id, h, w, l, count, p, wt, n, r);
	system("pause");
	cout << endl;

	//display menu to user
	cout << "Welcome to Weasleys' Wizard Wheezes." << endl;

	do{
		num = displayMenu();
		if (num == 1)
		{
			createCustomer(name, address);
		}

		else if (num == 2)
		{

			catalogChoice(cost, price1, num, id, count, value, cart, choice, cartItem, catalogItem, info);
		}

		else if (num == 3)
		{
			calculateTax(tax, taxCost, cityTax, cost, quantity, ship, height, weight, length, width, rate, vol, day, shipDay);
			cout.setf(ios::fixed);
			cout.precision(2);
			cout.setf(ios::fixed);
			cout.precision(2);
            cout << "The tax is $" << taxCost << "." << endl;
			calculateShipDay(day, shipDay, i, cost, height, weight, length, width, rate, vol, ship, tax, shipCost, cartItem);
			calculateSubtotal(subtotal, shipCost, cost, quantity);
			calculateTotal(tax, subtotal, total);
            cout << "Your final price is: $" << total + subtotal + tax << endl;
            break;
		}

		else if (num == 4)
		{
			cout << "Thank you for visiting Weasleys' Wizard Wheezes. Have a nice day!" << endl;
			system("pause");
			exit(1);
		}

		else if (num == 5)
		{
			testTax();
			testShipping();
			testSubtotal();
			testTotal();
		}

		else if (num == 6)
		{
			emptyCart(num, value, id, qtyChange, count, choice, cart, info, catalogItem, cartItem);
		}

		else cout << "You've chosen an invalid option. Please choose again." << endl;
	
	}while(num != 6);

	//validate purchase
	cout << "Are you sure you want to buy this? (Y or N)" << endl;
	cin >> choice;
	cin.clear();
	cin.ignore();
	cart = cart;
	if ((choice == 'y' || choice == 'Y'))
	{
		saveInvoice(cart, shipCost, taxCost, subtotal, name);
		cout << "Have a nice day!" << endl;
		system("pause");
		exit(1);
	}

	else if ((choice == 'n' || choice == 'N'))
	{
		cout << "Thanks for your time. Have a wonderful day!" << endl;
		system("pause");
		exit(1);
	}
	
	else cout << "Empty cart!" << endl;

	system("pause");
}
Topic archived. No new replies allowed.