Parallel Vectors and Text Files

Not sure how to put the specific calculations using the numbers from the text file
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 <vector>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;

struct Parts
{
	string number;
	char cls;
	int ohb;
	double cost;
};

// function prototypes
bool readFile(vector <Parts> &pVector);
void displayMenu();
double totalCost(const vector <Parts> &pVector);
/void countByClass(const vector <Parts> &pVector, vector <int> &classCounts);
double costForClass(char classIn, const vector<Parts> & pVector);
string highestCost(const vector<Parts> &pVector);
string lowestCost(const vector <Parts> &pVector);
void displayCounts(const vector <int> & classCounts); 



int main()
{
	int choice;
	string number;
	Parts cost;
	
	

	displayMenu();
	cin >> choice;
	if (choice == 1)
	{
		cout << "Your choice was 1" << endl;
		cout << "Total cost of inventory is" << " $" << totalCost(number, cost) << endl;
	}
	else if (choice = 2)
	{
		cout << "Your choice was 2" << endl;
		cout << "Count of parts by class" << endl;
	}
	else if (choice == 3)
	{
		cout << "Your choice was 3" << endl;
	}
	else if (choice == 4)
	{
		cout << "Your choice was 4" << endl;
	}
	else if (choice == 5)
	{
		cout << "Your choice was 5" << endl;
	}
	else if (choice == 6)
	{
		cout << "Your choice was 6" << endl;
	}
}

bool readFile(vector <Parts> &pVector)
{
	bool ok = true;
	bool no = false;
	ifstream inputFile; // Opens file
	inputFile.open("parts.txt"); // Opens required text
	Parts p;

	if (inputFile.fail())
	{
		cout << "Unable to open file!" << endl;
	}
	else
	{
		while (inputFile >> p.number >> p.cls >> p.ohb >> p.cost)
		{
			pVector.push_back(p);
		}
	}

	return ok;
}

void displayMenu()
{
	cout << "   R E P O R T S M E N U  " << endl;
	cout << "1. Total Cost of inventory. " << endl;
	cout << "2. A count of parts of each class. " << endl;
	cout << "3. Cost of inventory for a class. " << endl;
	cout << "4. Part with the highest cost of inventory. " << endl;
	cout << "5. Part with the lowest cost of inventory. " << endl;
	cout << "6. Exit. " << endl;


}
double totalCost(const vector <Parts> &pVector)
{
	cout << fixed << setprecision(2);
	// adding up the amounts of cost
	// choice 1
	Parts add;
	int total = 0;
	for (int i = 0; i < pVector.size(); i++)
	{
		total+= add.cost;
	}
	
	return total;
}
/void countByClass(const vector <Parts> &pVector, vector <int> &classCounts)
{
	// Displays letter and amount
	// choice 2
}
double costForClass(char classIn, const vector<Parts> & pVector)
{
	// Added amount for each individual class
	// choice 3
}
string highestCost(const vector<Parts> &pVector)
{
	// Which part had the highest cost
	// choice 4
}
string lowestCost(const vector <Parts> &pVector)
{
	// which part had the lowest cost
	// choice 5
}
void displayCounts(const vector <int> & classCounts)
{
	
}


Txt file:
P-39457 A 16 102.14
P-11702 B 21 24.74
P-11754 B 27 15.23
P-12009 D 3 12.80
P-12131 A 48 24.17
P-12385 B 41 27.33
P-12652 E 18 28.33
P-12677 D 15 26.59
P-12746 A 6 111.57
P-12746 C 25 14.83
P-12797 D 18 29.07
P-12856 A 33 120.96
P-13192 C 65 76.98
P-13289 C 45 36.61
P-13682 D 14 25.56
P-13713 A 10 35.12
P-14175 B 41 9.21
P-14226 D 14 28.95
P-14455 D 26 26.39
P-14493 A 29 22.09
P-14518 D 9 5.56
P-14613 A 49 74.66
P-14702 A 38 92.06
P-14748 C 24 24.80
P-38806 C 42 31.39
P-38870 A 29 127.19
P-38880 B 26 110.57
P-39014 A 30 111.33
Hello ohsimplyme,

Without testing the fist thing I see is the "totalCost" function. You define "total" as an "int" when it should be a "double" because you are adding a "double" to "total" and returning a "double" from the function. You define "Parts add;" yet add is an empty object whereas pVector has all the information you need. Thinking that the "totalCost" function should look like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
double totalCost(const vector <Parts> &pVector)
{
	cout << fixed << setprecision(2);
	// adding up the amounts of cost
	// choice 1

	double total = 0;

	for (int i = 0; i < pVector.size(); i++)
	{
		total += pVector[i].cost;
	}

	return total;
}


There is a start and an idea of how other functions should work.

Hope that helps,

Andy
Last edited on
Thank you Andy, but now my question is how would I format that in main, what i currently have shows up as an error.
if (choice == 1)
{
cout << "Your choice was 1" << endl;
cout << "Total cost of inventory is" << " $" <<totalCost(number, cost)<< endl;
}
Hello ohsimplyme,

After I loaded the file I found several problems.

1. The vector "pVector" needs to be defined in main and then passed to the functions that need it. In choice 1 the parameters "number" and "cost" should be "pVector" as described in the function prototype and the function definition.

2. It should be "==" not the asignmet "=". And the eventual function call there would look similar to what is in choice 1, the parameters part not the function call.

3. Lines 20 and 115 have a forward slash that should not be there.

4. The function "countByClass" takes two parameters, but I do not think the second parameter is needed as everything you need is in "pVector" and the vector "classCounts" I have not yet found where it is defined or populated. If the vector "classCounts" is needed it should be defined in main and built somewhere somehow.

If I find anything else I will let you know.

Hope that helps,

Andy
Line 19: Look at your function prototype and implementation for totalCost(). Both are expecting a vector.

Line 41: You're calling totalCost with a string and a struct.

 
  cout << "Total cost of inventory is" << " $" << totalCost(pVector) << endl;

Note that pVector is not currently defined in main(). You need to define it in main() so you can pass it to the other functions.

You also never call readFile(), to the vector would be empty until you do so.

Last edited on
So I made many adjustments to my code, this is currently how it's running. But I'm still having issues with the calculations.
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
#include <iostream>
#include <vector>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;

struct Parts
{
	string number;
	char cls;
	int ohb;
	double cost;
};

// function prototypes
bool readFile(vector <Parts> &pVector);
void displayMenu();
double totalCost(const vector <Parts> &pVector);
void countByClass(const vector <Parts> &pVector, vector <int> &classCounts);
double costForClass(char classIn, const vector<Parts> & pVector);
string highestCost(const vector<Parts> &pVector);
string lowestCost(const vector <Parts> &pVector);
void displayCounts(const vector <int> & classCounts); 




int main()
{
	int choice;
	char choiceAgain;
	string number;
	vector <Parts> p;
	int partVector;
	int classCount;
	int classIn;
	bool check;
	vector <int> classCounts;
	char x = 0;
	



	check = readFile(p);
	if (check == true)
	{
		displayMenu();
		cin >> choice;
	
			if (choice == 1)
			{
				cout << "Your choice was 1" << endl;
				cout << fixed << setprecision(2) << "Total cost of inventory is" << " $" << totalCost(p) << endl;
			}
			else if (choice == 2)
			{
				cout << "Your choice was 2" << endl;
				cout << "Count of parts by class" << endl;
				countByClass(p, classCounts);
				cout << endl;

			}
			else if (choice == 3)
			{
				cout << "Your choice was 3" << endl;
				cout << "Which class?" << endl;
				cin >> choiceAgain;
				cout << "Cost of inventory for class" << " " << choiceAgain << " is" << " $" << costForClass(x, p) << endl;
			}
			else if (choice == 4)
			{
				cout << "Your choice was 4" << endl;
				cout << "The part with the highest cost of inventory is" << " " << highestCost(p) << endl;
			}
			else if (choice == 5)
			{
				cout << "Your choice was 5" << endl;
				cout << "The part with the lowest cost of inventory is" << " " << lowestCost(p) << endl;
			}
			else if (choice == 6)
			{
				cout << "Your choice was 6 goodbye" << endl;
			}
		

	}
	else
	{
		cout << "Fail to open file, cannot continue" << endl;
	}
	return 0;
}

bool readFile(vector <Parts> &pVector)
{
	bool ok = true;
	bool no = false;
	ifstream inputFile; // Opens file
	inputFile.open("parts.txt"); // Opens required text
	Parts p;

	if (inputFile.fail())
	{
		return no;
	}
	else
	{
		while (inputFile >> p.number >> p.cls >> p.ohb >> p.cost)
		{
			pVector.push_back(p);
		}
	}

	return ok;
}

void displayMenu()
{
	cout << "   R E P O R T S M E N U  " << endl;
	cout << "1. Total Cost of inventory. " << endl;
	cout << "2. A count of parts of each class. " << endl;
	cout << "3. Cost of inventory for a class. " << endl;
	cout << "4. Part with the highest cost of inventory. " << endl;
	cout << "5. Part with the lowest cost of inventory. " << endl;
	cout << "6. Exit. " << endl;


}
double totalCost(const vector <Parts> &pVector)
{
	// adding up the amounts of cost
	// choice 1
	double total = 0;
	for (int i = 0; i < pVector.size(); i++)
	{
		total += pVector[i].cost;
	}
	
	return total;
}
void countByClass(const vector <Parts> &pVector, vector <int> &classCounts)
{
	Parts p;
	int counterA = 0;
	int counterB = 0;
	int counterC = 0;
	int counterD = 0;
	int counterE = 0;
	int counterF = 0;
	

	for (int i = 0; i < 1; i++)
	{
		switch (pVector[i].cls)
		{
		case ('A'):
			counterA += pVector[i].ohb;
			break;
		case ('B'):
			counterB += pVector[i].ohb;
			break;
		case ('C'):
			counterC += pVector[i].ohb;
			break;
		case ('D'):
			counterD += pVector[i].ohb;
			break;
		case ('E'):
			counterE += pVector[i].ohb;
			break;
		case ('F'):
			counterF += pVector[i].ohb;
			break;
		}
		
		cout << "A" << "  " << counterA << endl;
		cout << "B" << "  " << counterB << endl;
		cout << "C" << "  " << counterC << endl;
		cout << "D" << "  " << counterD << endl;
		cout << "E" << "  " << counterE << endl;
		cout << "F" << "  " << counterF;


	}
	// Displays letter and amount
	// choice 2
}

double costForClass(char classIn, const vector<Parts> & pVector)
{
	char choiceA = 0;
	char choiceB = 0;
	char choiceC = 0;
	char choiceD = 0;
	char choiceE = 0;
	char choiceF = 0;
	Parts p;
	double total = 0;
	total += p.ohb;

		switch (p.cls)
		{
		case ('A'):
			choiceA += total;
			break;
		case ('B'):
			choiceB += total;
			break;
		case ('C'):
			choiceC += total;
			break;
		case ('D'):
			choiceD += total;
			break;
		case ('E'):
			choiceE += total;
			break;
		case ('F'):
			choiceF += total;
			break;
		}
		cout << choiceA;
		cout << choiceB;
		cout << choiceC;
		cout << choiceD;
		cout << choiceE;
		cout << choiceF;

	//pVector.push_back(p); (not working, why?)

	// Added amount for each individual class
	// choice 3
		return total;
}
string highestCost(const vector<Parts> &pVector)
{
	// Which part had the highest cost
	// choice 4
}
string lowestCost(const vector <Parts> &pVector)
{
	// which part had the lowest cost
	// choice 5
}
void displayCounts(const vector <int> & classCounts)
{
	
}
Hello ohsimplyme,

Of course it is not printing the correct numbers.

void countByClass(const vector <Parts> &pVector, vector <int> &classCounts)

You have a second parameter here that is never used, so unless you have a later use for this I do not see any use for it right now. Line 144 "Parts p;" has no real use in this function because all the information you will need is in "pVector". The for loop online 153 will only have one iteration due to the middle condition. it should be
for (int i = 0; i < pVector.size(); i++) Starting at line 177 all the cout statements should be outside the for loop. And I would add an "endl" to the last "cout".

Now for double costForClass(char classIn, const vector<Parts> & pVector). Again you are sending two parameters the first parameter is never used. So what is its use? And does it need to be there?

Again you have defined "Parts p;", but never do anything with it, so when you have the line total += p.ohb; ohb has no value and total might equal whatever garbage is at the memory location of ohb. I have not tried it yet, but I think putting this
std::cout << " " << p.number << " " << p.cls << " " << p.ohb << " " << p.cost << std::endl;
after the definition of "p" will give you an idea of what is in "p". Not to keep this line its just for testing. The switch will not work because "cls" does not contain a useful value, so it will check every case and exit the switch without doing anything. The "default" case would be useful here. The function will only execute once because you have no means to loop through the vector.

Once you get the "countByClass" function working correctly this will give you something to follow for the"costForClass" function. They should look very similar.

Hope that helps,

Andy

Last edited on
Hello ohsimplyme,

If you make the changes to void countByClass(const vector <Parts> &pVector, vector <int> &classCounts) I mentioned earlier that should work.

After working with choice 3 in main and the function "costForClass" I found that choice 3 needed some work and the function needed a total rework. I reworked choice 3 this way:

1
2
3
4
5
6
7
8
9
else if (choice == 3)
{
	cout << "Your choice was 3" << endl;
	cout << "Which class? ";
	cin >> choiceAgain;
	choiceAgain = toupper(choiceAgain);
	std::cout << std::fixed << std::showpoint << std::setprecision(2);
	cout << "Cost of inventory for class" << " " << choiceAgain << " is" << " $" << costForClass(choiceAgain, p) << endl;
}


Although it works, "choiceAgain" is not the best name for this variable. It is a bit misleading, how can you "choiceAgain" when you have not made a first choice, "choice" would be a better name for the variable, IMHO. Just my thoughts on that the name of the variable does not matter as long as you understand it, but think about looking at this program six months from now when you discover that some of your variable names do not make any sense. Better to chose a variable name that is easy to understand in the beginning.

Line 6 makes sure you have an upper case letter to send to the function. My research says that "toupper" needs the header file "ctype.h" to be included at the beginning of the program.

Line 7 uses "iomanip", that you did include, to format how the number that the function will return.

The only problem with line 8 is how you call the function. Line 5 is where you get the letter to send to the function yet you call the function using "x" not "choiceAgain" as you should. If you check the value of "x" before you call the function it should be "0\0" which is not what you want. BTW you define "x" as a char and initialize it with a numeric value, not the correct way.

Now for the function " costForClass". Once I started working with it I did figure out what the first parameter is used for. Confusing in the beginning because you never used it. The definitions of char choiceA = 0; and the others. Not only are these variables initialized wrong they are not needed along with the switch. The switch really does not work here. If I understand what the function should do the code should be something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
double costForClass(char classIn, const vector<Parts> & pVector)
{
	// Added amount for each individual class
	// choice 3
	double total = 0.0;

	for (size_t lp = 0; lp < pVector.size(); lp++)
	{
		if (pVector[lp].cls == classIn)
		{
			total += pVector[lp].ohb*pVector[lp].cost;
		}
	}
	return total;
}


Some of the work I have done is a partial fix, Just enough to get it working. Still have more to check.

Hope that helps,

Andy
Hello ohsimplyme,

I came across a question that might be a problem. In the menu options it says 1. Total Cost of inventory., but in the function it says adding up the amounts of cost not quite the same. The function works fine for adding up the amounts of cost, but if you want 1. Total Cost of inventory. the code would be:

total += pVector[i].ohb * pVector[i].cost; which would tell you what all your inventory is worth.

Hope that helps,

Andy
Topic archived. No new replies allowed.