Simple store ordering assignment giving memory read errors.

This code should display an inventory for a user to enter a corresponding value to an item and make an order, and end the order after a set stop sequence. For some reason I am getting the following error once I get to the stop sequence. I then tried just forcing values into the arrays, skipping the ordering part and the error remains. I am very stumped.

"Exception thrown at 0x00CD2DA2 in xxxxxx.exe: 0xC0000005: Access violation reading location 0xCD40B310.

If there is a handler for this exception, the program may be safely continued."


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
#include<iostream>
#include<fstream>
#include<string>
#include<tuple>
#include<iomanip>


using namespace std;

tuple<string, float, char> readInventory(int selection);
void displayInventory();
void order();

int main()
{

	displayInventory();
	order();

	system("pause");
	return 0;
}


tuple<string, float, char> readInventory(int selection) // tuple used to allow me to call any of the 3 arrays 
{
	const int arraySize = 26; // setting array for 26 items in list
	string name[arraySize];
	float price[arraySize];
	char taxable[arraySize];

	ifstream inventory("E:\\Downloads\\Minimart.txt"); // reading store inventory from txt file. contains item name, item price, and code (N/T) weather the item is taxable or not

	for (int count = 0; count < arraySize; count++) // sending info from txt file to 3 parallel arrays
	{
		inventory >> name[count];
		inventory >> price[count];
		inventory >> taxable[count];
	}


	return make_tuple(name[selection], price[selection], taxable[selection]);
}

void displayInventory() // display all items in inventory and their prices
{
	cout << "~~~~~~ Current Inventory ~~~~~~" << endl << endl;


	for (int count = 0; count < 26; count++)
	{
		cout.width(5);
		cout << count << ". " << get<0>(readInventory(count)) << " " << fixed << setprecision(2);
		cout << "$" << get<1>(readInventory(count)) << endl;
	}
	cout << endl << "Enter item number to place order: "; // prompt user for selection 
}



void order()

{
	int tax[20], taxCount = 0, noTax[20], noTaxCount = 0; // made arrays tax to hold the selection value of all taxable items and same for non taxable
	float total, taxTotal = 0, noTaxTotal = 0, taxDue, taxRate = .07;

	int selection;

		cin >> selection;

	
	while (selection != -1) // check if selection is equal to my stop sequence and continue if not
	{
		while (selection >= 0 && selection <= 25) // check if selection was valid compared to list, continue if true
		{
	
			if (get<2>(readInventory(selection)) == 'N') // check if item is taxable or not
			{
				cout << get<0>(readInventory(selection)) << " " << fixed << setprecision(2) << "$" << get<1>(readInventory(selection)) << endl;
				noTax[noTaxCount] = selection; // assign item's selection code to no tax array
				noTaxCount++;
				cout << "Enter another selection or '-1' to finish: ";
				cin >> selection;
				if (selection == -1)
				break;
			}
			else // same for taxable items
			{
				cout << get<0>(readInventory(selection)) << " " << fixed << setprecision(2) << "$" << get<1>(readInventory(selection)) << "  Taxable" << endl;
				tax[taxCount] = selection;
				taxCount++;
				cout << "Enter another selection or '-1' to finish: ";
				cin >> selection;
				if (selection == -1)
				break;
			}
		
		}
			if (selection == -1)
			{
				selection = 0;
				break;
			}
		selection = 0;
		cout << "Invalid code, please try again: ";
		cin >> selection;
	}
	




	for (int count = 0; count < 20; count++) // use item selection values to check prices in inventory arrays
		taxTotal += get<1>(readInventory(tax[count]));

	for (int count = 0; count < 20; count++)
		noTaxTotal += get<1>(readInventory(noTax[count]));

	taxDue = taxTotal * taxRate;
	total = taxTotal + noTaxTotal;

	cout << "Tax Due: $" << fixed << setprecision(2) << taxDue << endl; 
	cout << "Total Due: $" << fixed << setprecision(2) << total << endl;
	cout << "Receipt is saved at ENTER LATER" << endl << endl;

	ofstream receipt("E:\\Downloads\\Receipt.txt"); //write reciept to txt file
	receipt << "Order Receipt" << endl << endl;


	for (int count = 0; count < 20; count++)
		cout << get<0>(readInventory(tax[count])) << "   " << get<1>(readInventory(tax[count])) << endl; // the cout here and on for easier debuging, will be changed to receipt

	for (int count = 0; count < 20; count++)
		cout << get<0>(readInventory(noTax[count])) << "   " << get<1>(readInventory(noTax[count])) << "Taxable" << endl;

	cout << "Tax Due: $" << fixed << setprecision(2) << taxDue << endl;
	cout << "Total Due: $" << fixed << setprecision(2) << total << endl;
}

Arrays int tax[20] and int noTax[20] are not initialised hence at lines 131 or 134 readInventory(tax[count]) or readInventory(noTax[count]) may pass a garbage selection code to the function.

You should probably keep track of how many elements have actually been filled with valid data, but in the meantime, initialise the arrays:
1
2
    int tax[20] = {};
    int noTax[20] = {};

Thank you!! That fixed it!
Topic archived. No new replies allowed.