Error with adding entry to array of structs

Hello. Currently my program is supposed to take a request from the user, depending on what letter they input. When the user enters the letter I, they should be able to enter a new item into the array. However after if (userChoice == 'I') completes, the new item isn't being added to the array, which I check with if (userChoice == 'D'). What mistake am I making here?

Thank you for your help! Learning a lot from this forum.

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
  #include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <cmath>
using namespace std;

struct Tools {
	string name;
	int amount;
	double price;
};

int search(Tools inventory[], string name, int length);
void remove(Tools inventory[], string name, int &length);

int main()
{
	char userChoice;
	int count = 0;
	ifstream fin;
	Tools inventory[100];

	fin.open("inventory.txt");
	while (!fin.eof()) {			//while not end of report, do the following:
		fin >> inventory[count].name >> inventory[count].amount >> inventory[count].price; //Reads in each set of text on the line and stores into appropriate variable
		count++;
	}
	fin.close();

	cout << "Select from menu:" << endl << "E - Edit" << endl << "I - Insert" << endl << "R - Remove" << endl << "D - Display Table" << endl << "Q - Quit";
	cout << endl << endl << "Enter letter of choice: ";
	cin >> userChoice;
	while (userChoice != 'Q') {
		if (userChoice == 'E') {
			string userProduct, posOrNeg, editValue;
			int spotInArray, currentInv, amountToChange;
			cout << "Enter product:";
			cin >> userProduct;
			spotInArray = search(inventory, userProduct, 100);

			if (spotInArray != -1) {
				cout << "Enter amount to add (+) or subtract (-):";
				cin >> editValue;
				currentInv = inventory[spotInArray].amount;
				posOrNeg = editValue.substr(0, 1);
				amountToChange = std::stoi(editValue.substr(1, 3));
				if (posOrNeg == "+") {
					inventory[spotInArray].amount = currentInv + amountToChange;
					cout << "Item " << userProduct << " has new amount in stock " << inventory[spotInArray].amount << endl << endl;
				}
				if (posOrNeg == "-") {
					inventory[spotInArray].amount = currentInv - amountToChange;
					cout << "Item " << userProduct << " has new amount in stock " << inventory[spotInArray].amount << endl << endl;
				}
			}
			if (spotInArray == -1) {
				cout << "Item " << userProduct << " is not found." << endl << endl;
			}

			cout << "Enter letter of choice: ";
			cin >> userChoice;

		}
		if (userChoice == 'I') {
			string prodName;
			int prodAmount, subArrayLength = 0, spotInArray;
			double prodPrice;
			subArrayLength = (4 * count) / 4;
			cout << "Enter new product ID, stock, and unit price: ";
			cin >> prodName >> prodAmount >> prodPrice;
			subArrayLength = subArrayLength + 4;
			spotInArray = search(inventory, prodName, 100);
			if (spotInArray == -1) {
				inventory[subArrayLength].name = prodName;
				inventory[subArrayLength].amount = prodAmount;
				inventory[subArrayLength].price = prodPrice;
				count++;
				cout << "Item " << prodName << " is inserted." << endl << endl;
			}
			if (spotInArray != -1) {
				cout << "Item " << prodName << " already exists." << endl << endl;
			}

			cout << "Enter letter of choice: ";
			cin >> userChoice;
		}
		if (userChoice == 'R') {
			string toRemove;
			int spotInArray;
			int subArrayLength = (4 * count) / 4;
			cout << "Enter ID of product to remove: ";
			cin >> toRemove;

			spotInArray = search(inventory, toRemove, subArrayLength);
			if (spotInArray != -1) {
				remove(inventory, toRemove, subArrayLength);//needs to be subarray length
				cout << "Item " << toRemove << " is removed." << endl << endl;
			}
			if (spotInArray == -1) {
				cout << "Item " << toRemove << " does not exist." << endl << endl;
			}
			cout << "Enter letter of choice: ";
			cin >> userChoice;
		}
		if (userChoice == 'D') {
			int counter = 0;
			double totalInventoryPrice = 0;
			while (inventory[counter].amount >= 0) { 
				cout << left;
				cout << setw(15) << inventory[counter].name << setw(15) << inventory[counter].amount << setw(15) << inventory[counter].price << endl;
				totalInventoryPrice = totalInventoryPrice + (inventory[counter].price * inventory[counter].amount);
				counter++;
			}
			cout << endl << "Total inventory: " << totalInventoryPrice << endl << endl;
			cout << "Enter letter of choice: ";
			cin >> userChoice;
		}
		if (userChoice == 'Q') {
			cout << endl;
		}
	}
	return 0;
}

int search(Tools inventory[], string name, int length) {
	int loc = -1;
	for (int i = 0; i < length; i++)
		if (inventory[i].name == name)
			loc = i;
	return loc;
}

void remove(Tools inventory[], string name, int &length)
// PRE: 0 <= len <= array size
// POST: The first occurrence of the target is removed from the array
{
	int res = search(inventory, name, 100);; // call search function on target
	if (res != -1) // if target is found ...
	{
		length--; // decrement subarray length
		inventory[res] = inventory[length]; // overwrite item with last value of subarray
	}
}
Last edited on
subArrayLength doesn't make sense. Use count instead and make sure that it is < 100.

Line 70 is actually a useless calculation.
On line 73 you always have an offset of 4.
What is the point of that?
Topic archived. No new replies allowed.