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